sdl_elm.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/usr/bin/python3
  2. import pygame
  3. import pygame.gfxdraw
  4. import pygame.font
  5. font0 = pygame.font.SysFont("freesans",10)
  6. font0b = pygame.font.SysFont("freesansbold",10)
  7. font = pygame.font.SysFont("freemonobold",22)
  8. font10 = pygame.font.SysFont("freemonobold",10)
  9. font12 = pygame.font.SysFont("freemonobold",12)
  10. font15 = pygame.font.SysFont("freemonobold",15)
  11. font22 = pygame.font.SysFont("FreeSans",22)
  12. #font = pygame.font.SysFont(None,30)
  13. class VALUE():
  14. def __init__(self,v,_min=0,_max=255):
  15. self._val = v
  16. self._max = _max
  17. self._min = _min
  18. def set(self,val):
  19. if val <= self.max and val >= self.min:
  20. self._val = val
  21. def _check(self):
  22. if self._val > self._max:
  23. self._val = self._max
  24. if self._val < self._min:
  25. self._val = self._min
  26. def inc(self,v):
  27. self._val += v
  28. self._check()
  29. def get(self):
  30. self._check()
  31. return self._val
  32. class ELEM_KILLGROUP():
  33. def __init__(self):
  34. self.data = []
  35. def insert(self,elm):
  36. if elm not in self.data:
  37. self.data.append(elm)
  38. return 1
  39. return 0
  40. def clean(self,elm):
  41. v = elm.val
  42. for i in self.data:
  43. i.clear()
  44. #elm.set(v)
  45. return v
  46. class ELEM_BUF():
  47. def __init__(self,kill=None):
  48. self.val = 0
  49. self.color = [0,255,0]
  50. self.color_on = [255,255,0]
  51. self.type="toggle" #"flash"
  52. self.killgroup = kill
  53. def get(self):
  54. return self.val
  55. def get_color(self):
  56. if self.val:
  57. return self.color_on
  58. return self.color
  59. def clean(self):
  60. self.val = 0
  61. def press(self):
  62. if self.type == "toggle":
  63. if self.val:
  64. self.val = 0
  65. else:
  66. self.val = 1
  67. if self.type == "flash":
  68. self.val = 1
  69. def release(self):
  70. if self.type == "flash":
  71. self.val = 0
  72. class Layout():
  73. def __init__(self,master):
  74. self.master = master
  75. def pack(self,**args):
  76. pass
  77. def grid(self,**args):
  78. pass
  79. def bind(self,**args):
  80. pass
  81. class Button():
  82. def __init__(self,window,pos):
  83. self.window = window
  84. self.event_pos = [0,0]
  85. self.font0 = pygame.font.SysFont("freesans",10)
  86. self.w = 20
  87. self.h = 10
  88. self.pos = pos
  89. self.btn1 = ELEM_BUF() #btn
  90. #self.btn1.type = "flash"
  91. self.btn1.color = [140,140,140]
  92. self.btn1.color_on = [255,0,0]
  93. self.btn2 = ELEM_BUF() #sel
  94. self.btn2.color = [120,120,120]
  95. self.val = VALUE(0,0,256)
  96. self.val_inc = 10
  97. self.__layout = Layout(self)
  98. self.pack = self.__layout.pack
  99. self.grid = self.__layout.grid
  100. self.bind = self.__layout.bind
  101. self.text = "line1\nline2"
  102. self.motion = 0
  103. self.type = "toggle" # flash, kill
  104. self.text2 = []
  105. def check(self):
  106. if 10:#dbg:
  107. self.text2 = []
  108. #self.text2.append(self.val)
  109. self.text2.append([self.btn1.get(),self.btn2.get()])
  110. self.text2.append(self.btn1.type)
  111. self._check_event()
  112. self._check_min_hight()
  113. def draw(self,text="GOBO1"):
  114. self.check()
  115. self.window.set_alpha(128)
  116. self._draw_bg()
  117. self._draw_font(text="")
  118. rgb = self.btn2.get_color()
  119. self._draw_bd(color=rgb)
  120. self._draw_bd(delta=-1)
  121. self._draw_bd(delta=-2,highlight=1,color=rgb)
  122. def get_rect(self):
  123. self.check()
  124. return self.pos[:]
  125. def _check_min_hight(self):
  126. c = 1+ self.text.count("\n") #+1
  127. c += len(self.text2)
  128. fr = self.font0.render("test_font_hight" ,1, (0,0,0))
  129. #self.window.blit(fr,(pos[0]+4,r))
  130. fr_r =fr.get_rect()
  131. h = (fr_r[3]+1)*c +6#8 #+8
  132. #print("-.",c,h,fr_r,self.pos,self.text)
  133. #input()
  134. if self.pos[3] < h:
  135. self.pos[3] = h #ah+20
  136. #self.bg = (0xff,0xaa,0xaa,127)
  137. def _get_surface_center(self):
  138. #text_rect = fr.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
  139. pass
  140. def _draw_bg(self):
  141. pos = self.pos
  142. #rgb = self.bg
  143. rgb = self.btn1.get_color()
  144. pygame.draw.rect(self.window,rgb,pos)
  145. def _draw_font(self,text=""):
  146. pos = self.pos
  147. a = pos[0]+4
  148. r = pos[1]+4
  149. v = "{:4.02f}".format(self.val.get())
  150. lines = self.text.split("\n")
  151. lines.extend(self.text2)
  152. for i in lines:
  153. i = str(i)
  154. if "<val>" in i:
  155. i = i.replace("<val>",v)
  156. fr = self.font0.render(i ,1, (0,0,0))
  157. fr_r=fr.get_rect()
  158. p2 = [pos[0]+4,r,fr_r[2],fr_r[3]]
  159. if 0:# dbg # bg highlight
  160. pygame.draw.rect(self.window,[0,0,255],p2)
  161. self.window.blit(fr,(a,r))
  162. r+=fr_r[3]+1
  163. def _check_event(self):
  164. self.bd = [125,125,125]
  165. self.motion = 0
  166. if self.event_pos[0] < self.pos[0]+1:
  167. return #continue
  168. if self.event_pos[0] > self.pos[0]+self.pos[2]-1:
  169. return #continue
  170. if self.event_pos[1] < self.pos[1]+1:
  171. return #continue
  172. if self.event_pos[1] > self.pos[1]+self.pos[3]-1:
  173. return #continue
  174. #print(">>",self.event_pos)
  175. self.bd = [200,200,200]
  176. self.motion = 1
  177. def _draw_bd(self,delta=0,highlight=0,color=[0,0,0]):
  178. pos = self.pos
  179. d = delta
  180. xpos = (
  181. (pos[0]-d ,pos[1]-d),
  182. (pos[0]+pos[2]+d-1 ,pos[1]-d),
  183. (pos[0]+pos[2]+d-1 ,pos[1]+pos[3]+d-1),
  184. (pos[0]-d ,pos[1]+pos[3]+d-1)
  185. )
  186. i_old = None
  187. ypos = []
  188. for i in xpos:
  189. if i_old:
  190. ypos.append( (i_old,i) )
  191. i_old = i
  192. ypos.append( (i_old,xpos[0]) )
  193. for i in ypos:
  194. #print("ypos",i)
  195. #pygame.draw.aaline(self.window,color,i[0],i[1],1)
  196. pygame.draw.aaline(self.window,color,i[0],i[1],1)
  197. def event(self,event=None):
  198. #print(self.pos)
  199. if "pos" in event.dict:
  200. self.event_pos = event.pos
  201. self._check_event()
  202. if self.motion:
  203. #print(self,"Motion.event",event)
  204. if "button" in event.dict:
  205. #print()
  206. #print("-","Button.event",event.dict,event.type,event.button,self.type)
  207. #print(type(event))
  208. mode = ""
  209. if event.type == 5:
  210. mode = "press"
  211. if event.type == 6:
  212. mode = "release"
  213. e = [event.button,mode]
  214. print("e",e)
  215. if e[0] in [1,3] and e[1] == "press":
  216. self.btn1.press()
  217. if e[0] in [1,3] and e[1] == "release":
  218. self.btn1.release()
  219. if e[0] in [2] and e[1] == "press":
  220. self.btn2.press()
  221. if e[0] in [2] and e[1] == "release":
  222. self.btn2.release()
  223. if e[0] in [4]: #mouse encoder
  224. self.val.inc(self.val_inc)
  225. if e[0] in [5]: #mouse encoder
  226. self.val.inc(-self.val_inc)
  227. def draw_box(window,pos1,pos2,color=[128,128,128],text=1):
  228. color = [200,0,0,127]
  229. if text:
  230. fr = font15.render("A" ,1, (200,200,200))
  231. window.blit(fr,pos1)
  232. fr = font15.render("B" ,1, (200,200,200))
  233. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  234. # h unten
  235. _pos1 = [pos1[0],pos2[1]]
  236. _pos2 = [pos2[0],pos2[1]]
  237. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  238. color = [255,255,0,127]
  239. # h rechts
  240. _pos1 = [pos2[0],pos1[1]]
  241. _pos2 = [pos2[0],pos2[1]]
  242. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  243. color = [0,200,0,127]
  244. # h links
  245. _pos1 = [pos1[0],pos1[1]]
  246. _pos2 = [pos1[0],pos2[1]]
  247. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  248. color = [0,0,200,127]
  249. # h oben
  250. _pos1 = [pos1[0],pos1[1]]
  251. _pos2 = [pos2[0],pos1[1]]
  252. pygame.draw.aaline(window,color,_pos1,_pos2,1)