-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.py
222 lines (193 loc) · 8.02 KB
/
ui.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import pygame
from constants import *
def translate(value, min1, max1, min2, max2):
return min2 + (max2-min2)*((value-min1)/(max1-min1))
class Button:
def __init__(self, text, position = (Width-230, 600) , w = 100, h= 50, border=10, color = (0, 0, 0), borderColor = (64, 123, 158)):
self.text = text
self.position = position
self.w = w
self.h = h
self.border = border
self.temp = color
self.color = color
self.borderColor = borderColor
self.font = 'freesansbold.ttf'
self.fontSize = 25
self.textColor = (255, 255, 255)
self.state = False
self.action = None
def HandleMouse(self, HoverColor = (100, 100, 100)):
m = pygame.mouse.get_pos()
self.state = False
if m[0] >= self.position[0] and m[0] <= self.position[0] + self.w:
if m[1] >= self.position[1] and m[1] <= self.position[1] + self.h:
self.color = HoverColor
if pygame.mouse.get_pressed()[0]:
self.color = (200, 200, 200)
if self.action == None:
self.state = True
else:
self.color = self.temp
else:
self.color = self.temp
def Render(self, screen):
self.HandleMouse()
font = pygame.font.Font(self.font, self.fontSize)
text = font.render(self.text, True, self.textColor)
textRect = text.get_rect()
textRect.center = (self.position[0]+self.w//2, self.position[1]+self.h//2)
if self.border > 0:
pygame.draw.rect(screen, self.borderColor, pygame.Rect(self.position[0] - self.border//2, self.position[1] - self.border//2, self.w + self.border, self.h + self.border))
pygame.draw.rect(screen, self.color, pygame.Rect(self.position[0], self.position[1], self.w, self.h))
screen.blit(text, textRect)
class Panel:
def __init__(self, position = (Width-350, 100), w= 345, h= 500, color=(8, 3, 12), alpha=128):
self.position = position
self.w = w
self.h = h
self.color = color
self.alpha = alpha
def Render(self, screen):
s = pygame.Surface((self.w, self.h))
s.set_alpha(self.alpha)
s.fill(self.color)
screen.blit(s, (self.position[0], self.position[1]))
#pygame.draw.rect(screen, self.color, pygame.Rect(self.position[0], self.position[1], self.w, self.h))
class ToggleButton:
def __init__(self, position= ((Width-200, 400)), w = 30, h=30, state=False, color=(40, 40, 10), activeColor=(240, 140, 60)):
self.position = position
self.w = w
self.h = h
self.clicked = False
self.state = state
self.temp = (activeColor, color)
self.activeColor = activeColor
self.color = color
def HandleMouse(self, HoverColor = (150, 120, 40)):
m = pygame.mouse.get_pos()
if m[0] >= self.position[0] and m[0] <= self.position[0] + self.w:
if m[1] >= self.position[1] and m[1] <= self.position[1] + self.h:
self.color = HoverColor
self.activeColor = HoverColor
if self.clicked:
self.state = not self.state
self.color = (255, 255, 255)
else:
self.color = self.temp[1]
self.activeColor =self.temp[0]
else:
self.color = self.temp[1]
self.activeColor =self.temp[0]
def Render(self, screen, clicked):
self.HandleMouse()
self.clicked = clicked
if self.state == True:
pygame.draw.rect(screen, self.activeColor, pygame.Rect(self.position[0], self.position[1], self.w, self.h))
else:
pygame.draw.rect(screen, self.color, pygame.Rect(self.position[0], self.position[1], self.w, self.h))
return self.state
class TextUI:
def __init__(self,text, position, fontColor, anchor='center'):
self.position = position
self.text = text
self.font = 'freesansbold.ttf'
self.anchor = anchor
self.fontSize = 18
self.fontColor = fontColor
def Render(self, screen):
font = pygame.font.Font(self.font, self.fontSize)
text = font.render(self.text, True, self.fontColor)
textRect = text.get_rect()
setattr(textRect, self.anchor, self.position)
#textRect.center = (self.position[0], self.position[1])
screen.blit(text, textRect)
class DigitInput:
def __init__(self,startingValue, position = (Width-320, 100), w= 300, h= 600, color=(8, 3, 12)):
self.position = position
self.text = str(startingValue)
self.fontColor = (255, 255, 255)
self.fontSize = 18
self.font = 'freesansbold.ttf'
self.w = w
self.h = h
self.color = color
self.value = int(self.text)
self.hoverEnter = False
def Check(self, backspace,val):
if self.hoverEnter == True:
if backspace == True:
if len(str(self.value)) <= 0 or len(str(self.value))-1 <= 0:
self.value = 0
else:
self.value = int(str(self.value)[:-1])
else:
if self.text.isdigit():
self.value = int(str(self.value) + str(self.text))
else:
for el in self.text:
if el.isdigit() != True:
self.text = self.text.replace(el, "")
backspace == False
self.text = ""
def updateText(self, val, pressed):
m = pygame.mouse.get_pos()
if m[0] >= self.position[0] and m[0] <= self.position[0] + self.w:
if m[1] >= self.position[1] and m[1] <= self.position[1] + self.h:
self.hoverEnter = True
if pressed == True:
self.text += val
else:
self.hoverEnter = False
val = ""
else:
self.hoverEnter = False
val = ""
def Render(self, screen, val, backspace, pressed):
self.updateText(val, pressed)
self.Check(backspace, val)
font = pygame.font.Font(self.font, self.fontSize)
text = font.render(str(self.value), True, self.fontColor)
textRect = text.get_rect()
textRect.center = (self.position[0]+self.w//2, self.position[1]+self.h//2)
pygame.draw.rect(screen, self.color, pygame.Rect(self.position[0], self.position[1], self.w, self.h))
screen.blit(text, textRect)
class Slider:
def __init__(self,x, y, val, min1, max1, length, h, max=500):
self.value = val
self.x = x
self.y = y
self.h = h
self.min1 = min1
self.max1 = max1
self.length = length
self.lineColor = (20, 10, 20)
self.rectradius = 10
self.temp_radius = self.rectradius
self.rectColor = (255, 255, 255)
self.v = 0.4
self.temp = self.lineColor
self.max = max
def Calculate(self, val):
self.v = translate(val, 0, self.length, 0, 1)
self.value = self.v * self.max
def HandleMouse(self):
mx, my = pygame.mouse.get_pos()
if mx >= self.x and mx <= self.x + self.length:
if my >= self.y and my <= self.y + self.h:
self.rectradius = 15
if pygame.mouse.get_pressed()[0]:
self.Calculate(mx - self.x)
else:
self.lineColor = self.temp
self.rectradius = self.temp_radius
else:
self.lineColor = self.temp
self.rectradius = self.temp_radius
def Render(self,screen):
self.HandleMouse()
pygame.draw.rect(screen, self.lineColor, pygame.Rect(self.x, self.y, self.length, self.h))
x = int((self.v * self.length) + self.x)
pygame.draw.rect(screen, self.rectColor, pygame.Rect(self.x, self.y, int( self.v * self.length), self.h))
pygame.draw.circle(screen, (130, 213, 151), (x, self.y + (self.rectradius/2)), self.rectradius)
return self.value