sdl_elm.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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="flash" #"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. def get_font_hight(font):
  82. fr = font.render("test_font_hight" ,1, (0,0,0))
  83. r = fr.get_rect()
  84. h = r[3]
  85. return h
  86. def draw_bd(pos=[0,0,10,10],delta=0):
  87. d = delta
  88. xpos = (
  89. (pos[0]-d ,pos[1]-d),
  90. (pos[0]+pos[2]+d-1 ,pos[1]-d),
  91. (pos[0]+pos[2]+d-1 ,pos[1]+pos[3]+d-1),
  92. (pos[0]-d ,pos[1]+pos[3]+d-1)
  93. )
  94. i_old = None
  95. ypos = []
  96. for i in xpos:
  97. if i_old:
  98. ypos.append( (i_old,i) )
  99. i_old = i
  100. ypos.append( (i_old,xpos[0]) )
  101. return ypos
  102. def check_area_v(v1,v2,event_v):#elm_pos,event_pos):
  103. if event_v < v1+1:
  104. return 0
  105. if event_v > v2-1:
  106. return 0
  107. return 1
  108. def check_area(pos,event_pos):
  109. v2 = pos[0]+pos[2]
  110. x = check_area_v(pos[0],v2,event_pos[0])
  111. v2 = pos[1]+pos[3]
  112. y = check_area_v(pos[1],v2,event_pos[1])
  113. if x and y:
  114. return 1
  115. def check_area2(R1,R2): #pos,mouse_box
  116. btn_box = R1[:] #btn_box
  117. r2 = R2[:] #mouse_box
  118. w=btn_box[2]
  119. h=btn_box[3]
  120. p1 = [btn_box[0],btn_box[1]]
  121. p4 = [btn_box[0]+w,btn_box[1]+h]
  122. if r2[0] > r2[2]:
  123. r2[0],r2[2] = r2[2],r2[0]
  124. if r2[1] > r2[3]:
  125. r2[1],r2[3] = r2[3],r2[1]
  126. x=0
  127. if r2[2] > p1[0] and r2[0] < p4[0]:
  128. x+=1
  129. y=0
  130. if r2[3] > p1[1] and r2[1] < p4[1]:
  131. y+=1
  132. if x and y:#> 4:
  133. print("btn",R1,"mouse",R2)
  134. print("btn",btn_box,"mouse",r2)
  135. print("area2",x,y)
  136. return 1
  137. class Button():
  138. def __init__(self,window,pos):
  139. self.window = window
  140. self.event_pos = [0,0]
  141. self.font0 = pygame.font.SysFont("freesans",10)
  142. self.w = 20
  143. self.h = 10
  144. self.pos = pos
  145. self.fader = 1
  146. self.btn1 = ELEM_BUF() # btn (background)
  147. self.btn1.color = [140,140,140]
  148. self.btn1.color_on = [255,0,0]
  149. self.btn2 = ELEM_BUF() # sel
  150. self.btn2.color = [120,120,120]
  151. self.btn2.type ="toggle"
  152. self.btn3 = ELEM_BUF() # mouse focus
  153. self.btn3.color = [100,100,100]
  154. self.btn3.color_on = [200,200,200]
  155. self.val = VALUE(0,0,256)
  156. self.val_inc = 4.4 #10
  157. self.__layout = Layout(self)
  158. self.pack = self.__layout.pack
  159. self.grid = self.__layout.grid
  160. self.bind = self.__layout.bind
  161. self.text = "line1\nline2"
  162. self.type = "toggle" # flash, kill
  163. self.text2 = []
  164. def check(self):
  165. if 0:#dbg:
  166. self.text2 = []
  167. #self.text2.append(self.val)
  168. self.text2.append([self.btn1.get(),self.btn2.get(),self.btn3.get()])
  169. self.text2.append(self.btn1.type)
  170. self._check_event()
  171. self._check_min_hight()
  172. def draw(self,text="GOBO1"):
  173. self.check()
  174. self.window.set_alpha(128)
  175. self._draw_bg()
  176. self._draw_fader()
  177. self._draw_font(text="")
  178. rgb = self.btn2.get_color()
  179. self._draw_bd(color=rgb)
  180. self._draw_bd(delta=-1)
  181. rgb = self.btn3.get_color()
  182. self._draw_bd(delta=-2,color=rgb)
  183. def get_rect(self):
  184. self.check()
  185. return self.pos[:]
  186. def _check_min_hight(self):
  187. c = 1+ self.text.count("\n") #+1
  188. c += len(self.text2)
  189. fh = get_font_hight(self.font0)
  190. h = (fh+1)*c +6#8 #+8
  191. if self.pos[3] < h:
  192. self.pos[3] = h #ah+20
  193. def _draw_bg(self):
  194. pos = self.pos
  195. rgb = self.btn1.get_color()
  196. pygame.draw.rect(self.window,rgb,pos)
  197. def _draw_fader(self):
  198. rgb = [0,200,0]
  199. pos2 = self.pos[:]
  200. v = self.val.get()
  201. fh = get_font_hight(self.font0)
  202. if self.fader:
  203. pos2[1] += 2 #fh+2
  204. pos2[3] = 4 #fh+2
  205. if v > 0:
  206. pos2[2] = int(pos2[2]* v/255)
  207. else:
  208. pos2[2] = 4
  209. pygame.draw.rect(self.window,rgb,pos2)
  210. def _draw_font(self,text=""):
  211. pos = self.pos
  212. a = pos[0]+4
  213. r = pos[1]+4
  214. v = "{:4.02f}".format(self.val.get())
  215. lines = self.text.split("\n")
  216. lines.extend(self.text2)
  217. for i in lines:
  218. i = str(i)
  219. if "<val>" in i:
  220. i = i.replace("<val>",v)
  221. fr = self.font0.render(i ,1, (0,0,0))
  222. fr_r=fr.get_rect()
  223. p2 = [pos[0]+4,r,fr_r[2],fr_r[3]]
  224. if 0:# dbg # bg highlight
  225. pygame.draw.rect(self.window,[0,0,255],p2)
  226. self.window.blit(fr,(a,r))
  227. r+=fr_r[3]+1
  228. def _set_mouse_focus(self,state):
  229. if state:
  230. self.btn3.press() # mouse focus on
  231. else:
  232. self.btn3.release()
  233. def _check_event(self):
  234. pass
  235. def _draw_bd(self,delta=0,color=[0,0,0]):
  236. l_pos = draw_bd(pos=self.pos,delta=delta)
  237. for i in l_pos:
  238. pygame.draw.aaline(self.window,color,i[0],i[1],1)
  239. def event(self,event=None):
  240. if "pos" in event.dict:
  241. self.event_pos = event.pos
  242. self._check_event()
  243. self._set_mouse_focus(0)
  244. if check_area(self.pos,self.event_pos):
  245. self._set_mouse_focus(1)
  246. if "button" in event.dict:
  247. mode = ""
  248. if event.type == 5:
  249. mode = "press"
  250. if event.type == 6:
  251. mode = "release"
  252. e = [event.button,mode]
  253. print("e",e)
  254. if e[0] in [1,3] and e[1] == "press":
  255. self.btn1.press()
  256. if e[0] in [1,3] and e[1] == "release":
  257. self.btn1.release()
  258. if e[0] in [2] and e[1] == "press":
  259. self.btn2.press()
  260. if e[0] in [2] and e[1] == "release":
  261. self.btn2.release()
  262. if e[0] in [4]: #mouse encoder
  263. self.val.inc(self.val_inc)
  264. if e[0] in [5]: #mouse encoder
  265. self.val.inc(-self.val_inc)
  266. def draw_mouse_box(window,pos1,pos2,color=[128,128,128],text=1):
  267. color = [200,0,0,127]
  268. if text:
  269. fr = font15.render("A" ,1, (200,200,200))
  270. window.blit(fr,pos1)
  271. fr = font15.render("B" ,1, (200,200,200))
  272. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  273. # h unten
  274. _pos1 = [pos1[0],pos2[1]]
  275. _pos2 = [pos2[0],pos2[1]]
  276. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  277. color = [255,255,0,127]
  278. # h rechts
  279. _pos1 = [pos2[0],pos1[1]]
  280. _pos2 = [pos2[0],pos2[1]]
  281. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  282. color = [0,200,0,127]
  283. # h links
  284. _pos1 = [pos1[0],pos1[1]]
  285. _pos2 = [pos1[0],pos2[1]]
  286. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  287. color = [0,0,200,127]
  288. # h oben
  289. _pos1 = [pos1[0],pos1[1]]
  290. _pos2 = [pos2[0],pos1[1]]
  291. pygame.draw.aaline(window,color,_pos1,_pos2,1)