GUI.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. import pygame
  2. class Label():
  3. def __init__(self, screen, text, x = 0, y = 0, text_color = (255, 255, 255), bg_color = None, font_size = 25):
  4. self.screen = screen
  5. self.text = text
  6. self.x = x
  7. self.y = y
  8. self.text_color = text_color
  9. self.bg_color = bg_color
  10. self.font_size = font_size
  11. self.font = pygame.font.Font(None, self.font_size)
  12. self.text_blit = self.font.render(self.text, True, self.text_color, self.bg_color)
  13. def set_text(self, text):
  14. self.text = text
  15. self.font = pygame.font.Font(None, self.font_size)
  16. self.text_blit = self.font.render(self.text, True, self.text_color, self.bg_color)
  17. def add_text(self, text):
  18. self.text += text
  19. self.font = pygame.font.Font(None, self.font_size)
  20. self.text_blit = self.font.render(self.text, True, self.text_color, self.bg_color)
  21. def set_text_color(self, text_color):
  22. self.text_color = text_color
  23. self.text_blit = self.font.render(self.text, True, self.text_color, self.bg_color)
  24. def set_bg_color(self, bg_color):
  25. self.bg_color = bg_color
  26. self.text_blit = self.font.render(self.text, True, self.text_color, self.bg_color)
  27. def set_x(self, x):
  28. self.x = x
  29. def set_y(self, y):
  30. self.y = y
  31. def move_x(self, x):
  32. self.x += x
  33. def move_y(self, y):
  34. self.y += y
  35. def get_text(self):
  36. return self.text
  37. def update(self):
  38. self.screen.blit(self.text_blit, (self.x, self.y))
  39. class Button():
  40. def __init__(self, screen, text, x = 0, y = 0, text_color = (0, 0, 0), bg_color = (200, 200, 200), font_size = 25, width = 100, height = 50, hover_color = (100, 100, 100)):
  41. self.pygame = pygame
  42. self.screen = screen
  43. self.text = text
  44. self.x = x
  45. self.y = y
  46. self.text_color = text_color
  47. self.bg_color = bg_color
  48. self.font_size = font_size
  49. self.width = width
  50. self.height = height
  51. self.hover_color = hover_color
  52. self.button = pygame.Rect(self.x, self.y, self.width, self.height)
  53. self.font = pygame.font.Font(None, self.font_size)
  54. self.text_blit = self.font.render(self.text, True, self.text_color)
  55. self.text_rect = self.text_blit.get_rect()
  56. self.xy = (self.button.centerx-self.text_rect.centerx, self.button.centery-self.text_rect.centery)
  57. def set_text(self, text):
  58. self.text = text
  59. self.font = pygame.font.Font(None, self.font_size)
  60. self.text_blit = self.font.render(self.text, True, self.text_color)
  61. self.text_rect = self.text_blit.get_rect()
  62. self.xy = (self.button.centerx-self.text_rect.centerx, self.button.centery-self.text_rect.centery)
  63. def set_text_color(self, text_color):
  64. self.text_color = text_color
  65. self.text_blit = self.font.render(self.text, True, self.text_color)
  66. self.text_rect = self.text_blit.get_rect()
  67. self.xy = (self.button.centerx-self.text_rect.centerx, self.button.centery-self.text_rect.centery)
  68. def set_bg_color(self, bg_color):
  69. self.bg_color = bg_color
  70. def set_x(self, x):
  71. self.x = x
  72. self.button = pygame.Rect(self.x, self.y, self.width, self.height)
  73. self.text_rect = self.text_blit.get_rect()
  74. self.xy = (self.button.centerx-self.text_rect.centerx, self.button.centery-self.text_rect.centery)
  75. def set_y(self, y):
  76. self.y = y
  77. self.button = pygame.Rect(self.x, self.y, self.width, self.height)
  78. self.text_rect = self.text_blit.get_rect()
  79. self.xy = (self.button.centerx-self.text_rect.centerx, self.button.centery-self.text_rect.centery)
  80. def move_x(self, x):
  81. self.x += x
  82. self.button = pygame.Rect(self.x, self.y, self.width, self.height)
  83. self.text_rect = self.text_blit.get_rect()
  84. self.xy = (self.button.centerx-self.text_rect.centerx, self.button.centery-self.text_rect.centery)
  85. def move_y(self, y):
  86. self.y += y
  87. self.button = pygame.Rect(self.x, self.y, self.width, self.height)
  88. self.text_rect = self.text_blit.get_rect()
  89. self.xy = (self.button.centerx-self.text_rect.centerx, self.button.centery-self.text_rect.centery)
  90. def set_hover_color(self, hover_color):
  91. self.hover_color = hover_color
  92. def active(self, key):
  93. if self.button.collidepoint(self.pygame.mouse.get_pos()) and key == 1:
  94. return True
  95. else:
  96. return False
  97. def update(self):
  98. if self.button.collidepoint(self.pygame.mouse.get_pos()):
  99. self.pygame.draw.rect(self.screen, self.hover_color, self.button)
  100. else:
  101. self.pygame.draw.rect(self.screen, self.bg_color, self.button)
  102. self.screen.blit(self.text_blit, self.xy)
  103. class Input():
  104. def __init__(self, screen, text = '', x = 0, y = 0, text_color = (0, 0, 0), circuit_color = (200, 200, 200), font_size = 25, width = 100, height = 50, bg_color = (100, 100, 100)):
  105. self.pygame = pygame
  106. self.screen = screen
  107. self.text = text
  108. self.x = x
  109. self.y = y
  110. self.text_color = text_color
  111. self.circuit_color = circuit_color
  112. self.font_size = font_size
  113. self.width = width
  114. self.height = height
  115. self.bg_color = bg_color
  116. self.input_text = pygame.Rect(self.x, self.y, self.width, self.height)
  117. self.font = pygame.font.Font(None, self.font_size)
  118. self.text_blit = self.font.render(self.text, True, self.text_color)
  119. self.text_rect = self.text_blit.get_rect()
  120. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  121. def active(self, key):
  122. #active(key = event.unicode)
  123. if key == "":
  124. self.text = self.text[0:-1]
  125. else:
  126. self.text += key
  127. self.text_blit = self.font.render(self.text, True, self.text_color)
  128. self.text_rect = self.text_blit.get_rect()
  129. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  130. def set_text_color(self, text_color):
  131. self.text_color = text_color
  132. self.text_blit = self.font.render(self.text, True, self.text_color)
  133. self.text_rect = self.text_blit.get_rect()
  134. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  135. def set_bg_color(self, circuit_color):
  136. self.circuit_color = circuit_color
  137. def set_x(self, x):
  138. self.x = x
  139. self.input_text = pygame.Rect(self.x, self.y, self.width, self.height)
  140. self.text_rect = self.text_blit.get_rect()
  141. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  142. def set_y(self, y):
  143. self.y = y
  144. self.input_text = pygame.Rect(self.x, self.y, self.width, self.height)
  145. self.text_rect = self.text_blit.get_rect()
  146. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  147. def move_x(self, x):
  148. self.x += x
  149. self.input_text = pygame.Rect(self.x, self.y, self.width, self.height)
  150. self.text_rect = self.text_blit.get_rect()
  151. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  152. def move_y(self, y):
  153. self.y += y
  154. self.input_text = pygame.Rect(self.x, self.y, self.width, self.height)
  155. self.text_rect = self.text_blit.get_rect()
  156. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  157. def set_hover_color(self, bg_color):
  158. self.bg_color = bg_color
  159. def set_text(self, text):
  160. self.text = text
  161. self.text_blit = self.font.render(self.text, True, self.text_color)
  162. self.text_rect = self.text_blit.get_rect()
  163. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  164. def get_text(self):
  165. return self.text
  166. def update(self):
  167. if self.input_text.collidepoint(self.pygame.mouse.get_pos()):
  168. self.text_blit = self.font.render(self.text + '|', True, self.text_color)
  169. self.text_rect = self.text_blit.get_rect()
  170. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  171. else:
  172. self.text_blit = self.font.render(self.text, True, self.text_color)
  173. self.text_rect = self.text_blit.get_rect()
  174. self.xy = (self.input_text.centerx-self.text_rect.centerx, self.input_text.centery-self.text_rect.centery)
  175. self.pygame.draw.rect(self.screen, self.bg_color, self.input_text)
  176. self.pygame.draw.rect(self.screen, self.circuit_color, self.input_text, 3)
  177. self.screen.blit(self.text_blit, self.xy)
  178. class Image():
  179. def __init__(self, screen, img, x = 0, y = 0):
  180. self.pygame = pygame
  181. self.screen = screen
  182. self.img = img
  183. self.x = x
  184. self.y = y
  185. self.image = self.pygame.image.load(self.img)
  186. def set_x(self, x):
  187. self.x = x
  188. def set_y(self, y):
  189. self.y = y
  190. def move_x(self, x):
  191. self.x += x
  192. def move_y(self, y):
  193. self.y += y
  194. def set_img(self, img):
  195. self.img = img
  196. self.image = self.pygame.image.load(self.img)
  197. def update(self):
  198. self.screen.blit(self.image, (self.x, self.y))
  199. class Toggle():
  200. def __init__(self, screen, value = False, x = 0, y = 0, width = 60, height = 25):
  201. self.screen = screen
  202. self.pygame = pygame
  203. self.value = value
  204. self.x = x
  205. self.y = y
  206. self.width = width
  207. self.height = height
  208. self.toggle = pygame.Rect(self.x, self.y, self.width, self.height)
  209. def active(self, key):
  210. if self.toggle.collidepoint(self.pygame.mouse.get_pos()) and key == 1:
  211. if self.value:
  212. self.value = False
  213. else:
  214. self.value = True
  215. def get_value(self):
  216. return self.value
  217. def set_value(self, value):
  218. self.value = value
  219. def set_x(self, x):
  220. self.x = x
  221. self.toggle = pygame.Rect(self.x, self.y, self.width, self.height)
  222. def set_y(self, y):
  223. self.y = y
  224. self.toggle = pygame.Rect(self.x, self.y, self.width, self.height)
  225. def move_x(self, x):
  226. self.x += x
  227. self.toggle = pygame.Rect(self.x, self.y, self.width, self.height)
  228. def move_y(self, y):
  229. self.y += y
  230. self.toggle = pygame.Rect(self.x, self.y, self.width, self.height)
  231. def update(self):
  232. if self.value:
  233. self.value_rect = pygame.Rect(self.x+self.width/2, self.y, self.width/2, self.height)
  234. self.value_color = (100, 255, 100)
  235. else:
  236. self.value_rect = pygame.Rect(self.x, self.y, self.width/2, self.height)
  237. self.value_color = (255, 100, 100)
  238. self.pygame.draw.rect(self.screen, (100, 100, 100), self.toggle, 3)
  239. self.pygame.draw.rect(self.screen, (100, 100, 100), self.toggle)
  240. self.pygame.draw.rect(self.screen, self.value_color, self.value_rect)
  241. class ProgressBar():
  242. def __init__(self, screen, progress = 0, x = 0, y = 0, width = 300, height = 30):
  243. self.screen = screen
  244. self.pygame = pygame
  245. self.progress = progress
  246. self.x = x
  247. self.y = y
  248. self.height = height
  249. self.width = width
  250. self.progressbar = pygame.Rect(self.x, self.y, self.width, self.height)
  251. self.progressbar_line = pygame.Rect(self.x, self.y, self.progress*self.width/100, self.height)
  252. self.font = pygame.font.Font(None, 32)
  253. self.text_blit = self.font.render(str(self.progress)+' %', True, (0, 0, 0))
  254. self.text_rect = self.text_blit.get_rect()
  255. self.xy = (self.progressbar.centerx-self.text_rect.centerx, self.progressbar.centery-self.text_rect.centery)
  256. def move_progress(self, progress):
  257. if self.progress < 100:
  258. self.progress += progress
  259. self.text_blit = self.font.render(str(self.progress)+' %', True, (0, 0, 0))
  260. self.progressbar = pygame.Rect(self.x, self.y, self.width, self.height)
  261. self.progressbar_line = pygame.Rect(self.x, self.y, self.progress*self.width/100, self.height)
  262. def set_x(self, x):
  263. self.x = x
  264. self.progressbar = pygame.Rect(self.x, self.y, self.width, self.height)
  265. self.progressbar_line = pygame.Rect(self.x, self.y, self.progress*self.width/100, self.height)
  266. self.font = pygame.font.Font(None, 32)
  267. self.text_blit = self.font.render(str(self.progress)+' %', True, (0, 0, 0))
  268. self.text_rect = self.text_blit.get_rect()
  269. self.xy = (self.progressbar.centerx-self.text_rect.centerx, self.progressbar.centery-self.text_rect.centery)
  270. def set_y(self, y):
  271. self.y = y
  272. self.progressbar = pygame.Rect(self.x, self.y, self.width, self.height)
  273. self.progressbar_line = pygame.Rect(self.x, self.y, self.progress*self.width/100, self.height)
  274. self.font = pygame.font.Font(None, 32)
  275. self.text_blit = self.font.render(str(self.progress)+' %', True, (0, 0, 0))
  276. self.text_rect = self.text_blit.get_rect()
  277. self.xy = (self.progressbar.centerx-self.text_rect.centerx, self.progressbar.centery-self.text_rect.centery)
  278. def move_x(self, x):
  279. self.x += x
  280. self.progressbar = pygame.Rect(self.x, self.y, self.width, self.height)
  281. self.progressbar_line = pygame.Rect(self.x, self.y, self.progress*self.width/100, self.height)
  282. self.font = pygame.font.Font(None, 32)
  283. self.text_blit = self.font.render(str(self.progress)+' %', True, (0, 0, 0))
  284. self.text_rect = self.text_blit.get_rect()
  285. self.xy = (self.progressbar.centerx-self.text_rect.centerx, self.progressbar.centery-self.text_rect.centery)
  286. def move_y(self, y):
  287. self.y += y
  288. self.progressbar = pygame.Rect(self.x, self.y, self.width, self.height)
  289. self.progressbar_line = pygame.Rect(self.x, self.y, self.progress*self.width/100, self.height)
  290. self.font = pygame.font.Font(None, 32)
  291. self.text_blit = self.font.render(str(self.progress)+' %', True, (0, 0, 0))
  292. self.text_rect = self.text_blit.get_rect()
  293. self.xy = (self.progressbar.centerx-self.text_rect.centerx, self.progressbar.centery-self.text_rect.centery)
  294. def update(self):
  295. self.pygame.draw.rect(self.screen, (100, 100, 100), self.progressbar)
  296. self.pygame.draw.rect(self.screen, (100, 255, 100), self.progressbar_line)
  297. self.screen.blit(self.text_blit, self.xy)
  298. class Stepper():
  299. def __init__(self, screen, value = 0, step = 1, height = 25, width = 120, x = 0, y = 0):
  300. self.screen = screen
  301. self.pygame = pygame
  302. self.value = value
  303. self.height = height
  304. self.width = width
  305. self.step = step
  306. self.x = x
  307. self.y = y
  308. self.stepper = pygame.Rect(self.x, self.y, self.width, self.height)
  309. self.up = pygame.Rect(self.x+self.width*2/3, self.y, self.width/3, self.height)
  310. self.down = pygame.Rect(self.x, self.y, self.width/3, self.height)
  311. self.font = pygame.font.Font(None, 32)
  312. self.text_blit = self.font.render(str(self.value), True, (0, 0, 0))
  313. self.text_rect = self.text_blit.get_rect()
  314. self.xy = (self.stepper.centerx-self.text_rect.centerx, self.stepper.centery-self.text_rect.centery)
  315. def active(self, key):
  316. if self.down.collidepoint(self.pygame.mouse.get_pos()) and key == 1:
  317. self.value -= self.step
  318. self.text_blit = self.font.render(str(self.value), True, (0, 0, 0))
  319. self.text_rect = self.text_blit.get_rect()
  320. self.xy = (self.stepper.centerx-self.text_rect.centerx, self.stepper.centery-self.text_rect.centery)
  321. elif self.up.collidepoint(self.pygame.mouse.get_pos()) and key == 1:
  322. self.value += self.step
  323. self.text_blit = self.font.render(str(self.value), True, (0, 0, 0))
  324. self.text_rect = self.text_blit.get_rect()
  325. self.xy = (self.stepper.centerx-self.text_rect.centerx, self.stepper.centery-self.text_rect.centery)
  326. def set_x(self, x):
  327. self.x = x
  328. self.stepper = pygame.Rect(self.x, self.y, self.width, self.height)
  329. self.up = pygame.Rect(self.x+self.width*2/3, self.y, self.width/3, self.height)
  330. self.down = pygame.Rect(self.x, self.y, self.width/3, self.height)
  331. self.font = pygame.font.Font(None, 32)
  332. self.text_blit = self.font.render(str(self.value), True, (0, 0, 0))
  333. self.text_rect = self.text_blit.get_rect()
  334. self.xy = (self.stepper.centerx-self.text_rect.centerx, self.stepper.centery-self.text_rect.centery)
  335. def set_y(self, y):
  336. self.y = y
  337. self.stepper = pygame.Rect(self.x, self.y, self.width, self.height)
  338. self.up = pygame.Rect(self.x+self.width*2/3, self.y, self.width/3, self.height)
  339. self.down = pygame.Rect(self.x, self.y, self.width/3, self.height)
  340. self.font = pygame.font.Font(None, 32)
  341. self.text_blit = self.font.render(str(self.value), True, (0, 0, 0))
  342. self.text_rect = self.text_blit.get_rect()
  343. self.xy = (self.stepper.centerx-self.text_rect.centerx, self.stepper.centery-self.text_rect.centery)
  344. def move_x(self, x):
  345. self.x += x
  346. self.stepper = pygame.Rect(self.x, self.y, self.width, self.height)
  347. self.up = pygame.Rect(self.x+self.width*2/3, self.y, self.width/3, self.height)
  348. self.down = pygame.Rect(self.x, self.y, self.width/3, self.height)
  349. self.font = pygame.font.Font(None, 32)
  350. self.text_blit = self.font.render(str(self.value), True, (0, 0, 0))
  351. self.text_rect = self.text_blit.get_rect()
  352. self.xy = (self.stepper.centerx-self.text_rect.centerx, self.stepper.centery-self.text_rect.centery)
  353. def move_y(self, y):
  354. self.y += y
  355. self.stepper = pygame.Rect(self.x, self.y, self.width, self.height)
  356. self.up = pygame.Rect(self.x+self.width*2/3, self.y, self.width/3, self.height)
  357. self.down = pygame.Rect(self.x, self.y, self.width/3, self.height)
  358. self.font = pygame.font.Font(None, 32)
  359. self.text_blit = self.font.render(str(self.value), True, (0, 0, 0))
  360. self.text_rect = self.text_blit.get_rect()
  361. self.xy = (self.stepper.centerx-self.text_rect.centerx, self.stepper.centery-self.text_rect.centery)
  362. def update(self):
  363. self.pygame.draw.rect(self.screen, (100, 100, 100), self.stepper)
  364. self.pygame.draw.rect(self.screen, (255, 100, 100), self.down)
  365. self.pygame.draw.rect(self.screen, (100, 255, 100), self.up)
  366. self.screen.blit(self.text_blit, self.xy)