sdl_elm.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #!/usr/bin/python3
  2. import pygame
  3. import pygame.gfxdraw
  4. import pygame.font
  5. from lib.xcolor import *
  6. font0 = pygame.font.SysFont("freesans",10)
  7. font0b = pygame.font.SysFont("freesansbold",10)
  8. font = pygame.font.SysFont("freemonobold",22)
  9. font10 = pygame.font.SysFont("freemonobold",10)
  10. font12 = pygame.font.SysFont("freemonobold",12)
  11. font15 = pygame.font.SysFont("freemonobold",15)
  12. font22 = pygame.font.SysFont("FreeSans",22)
  13. #font = pygame.font.SysFont(None,30)
  14. class VALUE():
  15. def __init__(self,v=0,_min=0,_max=255):
  16. self._val = v
  17. self._max = _max
  18. self._min = _min
  19. def _check(self):
  20. if self._val > self._max:
  21. self._val = self._max
  22. if self._val < self._min:
  23. self._val = self._min
  24. def get(self):
  25. self._check()
  26. return self._val
  27. def set(self,val):
  28. if val <= self._max and val >= self._min:
  29. self._val = val
  30. def inc(self,v):
  31. self._val += v
  32. self._check()
  33. class ELEM_KILLGROUP():
  34. def __init__(self):
  35. self.data = []
  36. def insert(self,elm):
  37. if elm not in self.data:
  38. self.data.append(elm)
  39. return 1
  40. return 0
  41. def clean(self,elm):
  42. v = elm.val
  43. for i in self.data:
  44. i.clear()
  45. #elm.set(v)
  46. return v
  47. class CALLBACK():
  48. def __init__(self):
  49. self._cb = self.dummy
  50. self.ok = 0
  51. def cb(self,*args):
  52. if self.ok:
  53. print("CALLBACK.cb",args)
  54. #try:
  55. self._cb(args)
  56. #except Exception as e:
  57. # print(" Exception CALLBACK.cb",args)
  58. def dummy(self,arg):
  59. print("CALLBACK.dummy",arg)
  60. def set(self,cb):
  61. self._cb = cb
  62. self.ok = 1
  63. print("CALLBACK",cb)
  64. class ELEM_BUF():
  65. def __init__(self,kill=None,name="ELEM_BUF"):
  66. self.val = VALUE() #0
  67. self.increment = 10
  68. self.name = name
  69. self.cb_on = CALLBACK()
  70. self.cb_off = CALLBACK()
  71. self.nr_on = [0]
  72. self.nr_off = [0]
  73. self.color = [0,255,0]
  74. self.color_on = [255,255,0]
  75. self.type="flash" #"toggle" #"flash",fade
  76. self.killgroup = kill
  77. self.events = []
  78. def _rep__(self):
  79. x="<ELEM_BUF name:{} val:{} id:{}>".format(self.name,self.val.get(), id(self))
  80. def get_event(self):
  81. out = self.events[:]
  82. self.events = []
  83. return out
  84. def get(self):
  85. return self.val.get()
  86. def get_color(self):
  87. if self.val.get():
  88. return self.color_on
  89. return self.color
  90. def clean(self):
  91. self.val.set(0)
  92. def press(self):
  93. #print("ELEM_BUF.press",[self.name,self.type,self.val.get()])
  94. if self.type == "fader":
  95. self.inc(self.increment)
  96. if self.type == "toggle":
  97. if self.val.get():
  98. self.val.set(0)
  99. else:
  100. self.val.set(1)
  101. if self.type == "flash":
  102. self.val.set(1)
  103. self.events.append("press")
  104. self.cb_on.cb("ho")
  105. def release(self):
  106. if self.type == "fader":
  107. self.inc(-self.increment)
  108. if self.type == "flash":
  109. self.val.set(0)
  110. self.events.append("release")
  111. def inc(self,v):
  112. self.val.inc(v)
  113. class Layout():
  114. def __init__(self,master):
  115. self.master = master
  116. def pack(self,**args):
  117. pass
  118. def grid(self,**args):
  119. pass
  120. def bind(self,**args):
  121. pass
  122. def get_font_hight(font):
  123. fr = font.render("test_font_hight" ,1, (0,0,0))
  124. r = fr.get_rect()
  125. h = r[3]
  126. return h
  127. def draw_bd(pos=[0,0,10,10],delta=0):
  128. d = delta
  129. xpos = (
  130. (pos[0]-d ,pos[1]-d),
  131. (pos[0]+pos[2]+d-1 ,pos[1]-d),
  132. (pos[0]+pos[2]+d-1 ,pos[1]+pos[3]+d-1),
  133. (pos[0]-d ,pos[1]+pos[3]+d-1)
  134. )
  135. i_old = None
  136. ypos = []
  137. for i in xpos:
  138. if i_old:
  139. ypos.append( (i_old,i) )
  140. i_old = i
  141. ypos.append( (i_old,xpos[0]) )
  142. return ypos
  143. def check_area_v(v1,v2,event_v):#elm_pos,event_pos):
  144. if event_v < v1+1:
  145. return 0
  146. if event_v > v2-1:
  147. return 0
  148. return 1
  149. def check_area(pos,event_pos):
  150. v2 = pos[0]+pos[2]
  151. x = check_area_v(pos[0],v2,event_pos[0])
  152. v2 = pos[1]+pos[3]
  153. y = check_area_v(pos[1],v2,event_pos[1])
  154. if x and y:
  155. return 1
  156. def check_area2_dir(R1,R2):
  157. r2 = R2[:] #mouse_box
  158. xd = 1
  159. yd = 1
  160. if r2[0] > r2[2]:
  161. r2[0],r2[2] = r2[2],r2[0]
  162. xd=-1
  163. if r2[1] > r2[3]:
  164. r2[1],r2[3] = r2[3],r2[1]
  165. yd=-1
  166. #print("check_area2_dir",xd,yd)
  167. return xd,yd,r2
  168. def check_area2(R1,R2): #pos,mouse_box
  169. xd,yd,r2 = check_area2_dir(R1,R2)
  170. btn_box = R1[:] #btn_box
  171. w=btn_box[2]
  172. h=btn_box[3]
  173. p1 = [btn_box[0],btn_box[1]]
  174. p4 = [btn_box[0]+w,btn_box[1]+h]
  175. x=0
  176. if r2[2] > p1[0] and r2[0] < p4[0]:
  177. x+=1
  178. y=0
  179. if r2[3] > p1[1] and r2[1] < p4[1]:
  180. y+=1
  181. if x and y:#> 4:
  182. #print("btn",R1,"mouse",R2)
  183. #print("btn",btn_box,"mouse",r2)
  184. #print("area2",x,y)
  185. return 1
  186. class Button():
  187. def __init__(self,window,pos):
  188. self.window = window
  189. self.event_pos = [0,0]
  190. self.font0 = pygame.font.SysFont("freesans-bold",16)
  191. self.w = 20
  192. self.h = 10
  193. self.pos = pos
  194. self.fader = "h" #v
  195. self.ATTR = "XX"
  196. self.ID = "0"
  197. self.btn1 = ELEM_BUF()
  198. self.btn1.name = "BUTTON"
  199. self.btn1.nr_on = [1,3]
  200. self.btn1.nr_off = [1,3]
  201. #self.btn1.color = LIGHTGRAY
  202. self.btn1.color = GRAY
  203. self.btn1.color_on = RED
  204. self.btn2 = ELEM_BUF() # sel elem
  205. self.btn2.name = "SELECT BUF"
  206. self.btn2.nr_on = [2]
  207. self.btn2.nr_off = [0]
  208. self.btn2.color = GRAY
  209. self.btn2.color_on = YELLOW
  210. self.btn2.type = "toggle"
  211. self.btn3 = ELEM_BUF()
  212. self.btn3.name = "MOUSE FOCUS"
  213. self.btn3.color = GRAY
  214. self.btn3.color_on = WHITHE
  215. self.btn4 = ELEM_BUF()
  216. self.btn4.name = "MOUSE ENCODER"
  217. self.btn4.increment = 4.4
  218. self.btn4.type = "fader"
  219. self.btn4.nr_on = [4]
  220. self.btn4.nr_off = [5]
  221. self.btn4.color = GRAY
  222. self.btn4.color_on = WHITHE
  223. self.btns = []
  224. self.btns.append(self.btn1)
  225. self.btns.append(self.btn2)
  226. self.btns.append(self.btn3)
  227. self.btns.append(self.btn4)
  228. self.__layout = Layout(self)
  229. self.pack = self.__layout.pack
  230. self.grid = self.__layout.grid
  231. self.bind = self.__layout.bind
  232. self.text = "line1\nline2"
  233. self.type = "toggle" # flash, kill
  234. self.dbg = 0
  235. self.text2 = []
  236. def __repr__(self):
  237. x="<sdl.BUTTON name:{} ID:{}-{:8} btn1:{:03} val2:{:03} id at {}>".format(self.btn1.name,self.ID,self.ATTR,self.btn1.val.get(),self.btn4.val.get(), id(self))
  238. return x
  239. def check(self):
  240. if self.dbg:
  241. self.text2 = []
  242. #self.text2.append(self.val)
  243. b = []
  244. for btn in self.btns:
  245. b.append(btn.get())
  246. self.text2.append(b)
  247. self.text2.append(self.btn1.type)
  248. self._check_event()
  249. self._check_min_hight()
  250. def draw(self,text="GOBO1"):
  251. self.check()
  252. self.window.set_alpha(128)
  253. self._draw_bg()
  254. self._draw_fader()
  255. self._draw_font(text="")
  256. rgb = self.btn2.get_color()
  257. self._draw_bd(color=rgb)
  258. self._draw_bd(delta=-1)
  259. rgb = self.btn3.get_color()
  260. self._draw_bd(delta=-2,color=rgb)
  261. def get_rect(self):
  262. self.check()
  263. return self.pos[:]
  264. def _check_min_hight(self):
  265. c = 1+ self.text.count("\n") #+1
  266. c += len(self.text2)
  267. fh = get_font_hight(self.font0)
  268. h = (fh+1)*c +6#8 #+8
  269. if self.pos[3] < h:
  270. self.pos[3] = h #ah+20
  271. def _draw_bg(self):
  272. pos = self.pos
  273. rgb = self.btn1.get_color()
  274. pygame.draw.rect(self.window,rgb,pos)
  275. def _draw_fader(self):
  276. rgb = [0,200,0]
  277. rgb = self.btn4.color_on
  278. pos2 = self.pos[:]
  279. hight = pos2[3]
  280. v = self.btn4.val.get() #self.val.get()
  281. fh = get_font_hight(self.font0)
  282. _max_val = self.btn4.val._max
  283. if self.fader == "h":
  284. pos2[1] += 2 #fh+2
  285. pos2[3] = 4 #fh+2
  286. if v > 0:
  287. pos2[2] = int(pos2[2]* v/_max_val)
  288. else:
  289. pos2[2] = 4
  290. pygame.draw.rect(self.window,rgb,pos2)
  291. elif self.fader == "v":
  292. if v > 0:
  293. pos2[1] += int((hight-20)* v/_max_val)
  294. pos2[3] = 20
  295. else:
  296. pos2[3] = 20
  297. pos2[0] += 6
  298. pos2[2] -= 12
  299. pygame.draw.rect(self.window,rgb,pos2)
  300. def _draw_font(self,text=""):
  301. pos = self.pos
  302. a = pos[0]+4
  303. r = pos[1]+4
  304. v = "{:4.02f}".format(self.btn4.val.get())
  305. lines = self.text.split("\n")
  306. lines.extend(self.text2)
  307. for i in lines:
  308. i = str(i)
  309. if "<ival%>" in i:
  310. v=float(v)
  311. v=v/self.btn4.val._max*100
  312. v=int(v)
  313. i = i.replace("<ival%>",str(v))
  314. if "<ival>" in i:
  315. i = i.replace("<ival>",str(int(float(v))))
  316. if "<val>" in i:
  317. i = i.replace("<val>",v)
  318. fr = self.font0.render(i ,1, (0,0,0))
  319. fr_r=fr.get_rect()
  320. p2 = [pos[0]+4,r,fr_r[2],fr_r[3]]
  321. if 0:# dbg # bg highlight
  322. pygame.draw.rect(self.window,[0,0,255],p2)
  323. self.window.blit(fr,(a,r))
  324. r+=fr_r[3]+1
  325. def _set_mouse_focus(self,state):
  326. if state:
  327. self.btn3.press() # mouse focus on
  328. else:
  329. self.btn3.release()
  330. def _check_event(self):
  331. pass
  332. def _draw_bd(self,delta=0,color=GRAY):#BLACK):
  333. l_pos = draw_bd(pos=self.pos,delta=delta)
  334. for i in l_pos:
  335. pygame.draw.aaline(self.window,color,i[0],i[1],1)
  336. def event(self,event=None):
  337. r_event = {}
  338. if "pos" in event.dict:
  339. self.event_pos = event.pos
  340. self._check_event()
  341. self._set_mouse_focus(0)
  342. if check_area(self.pos,self.event_pos):
  343. self._set_mouse_focus(1)
  344. if "button" in event.dict:
  345. mode = ""
  346. if event.type == 5:
  347. mode = "press"
  348. if event.type == 6:
  349. mode = "release"
  350. e = [event.button,mode]
  351. #print("e",e)
  352. for btn in self.btns:
  353. if e[0] in btn.nr_on and e[1] == "press":
  354. btn.press()
  355. if e[0] in btn.nr_off and e[1] == "release":
  356. btn.release()
  357. re = btn.get_event()
  358. if re and btn.name not in ['MOUSE FOCUS']:
  359. print("----------------",btn.name,re)
  360. r_event[btn.name] = re
  361. return r_event
  362. def draw_mouse_box(window,pos1,pos2,color=[128,128,128],text=1):
  363. color = [200,0,0,127]
  364. if text:
  365. fr = font15.render("A" ,1, (200,200,200))
  366. window.blit(fr,pos1)
  367. fr = font15.render("B" ,1, (200,200,200))
  368. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  369. # h unten
  370. _pos1 = [pos1[0],pos2[1]]
  371. _pos2 = [pos2[0],pos2[1]]
  372. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  373. color = [255,255,0,127]
  374. # h rechts
  375. _pos1 = [pos2[0],pos1[1]]
  376. _pos2 = [pos2[0],pos2[1]]
  377. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  378. color = [0,200,0,127]
  379. # h links
  380. _pos1 = [pos1[0],pos1[1]]
  381. _pos2 = [pos1[0],pos2[1]]
  382. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  383. color = [0,0,200,127]
  384. # h oben
  385. _pos1 = [pos1[0],pos1[1]]
  386. _pos2 = [pos2[0],pos1[1]]
  387. pygame.draw.aaline(window,color,_pos1,_pos2,1)