fix.py 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. #!/usr/bin/python3
  2. import time
  3. import traceback
  4. boot = time.time()
  5. from collections import OrderedDict
  6. import random
  7. import os
  8. import sys
  9. #sys.path.insert(0,os.path.realpath(os.getcwd() + '/..'))
  10. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  11. print(sys.path)
  12. print()
  13. CAPTION = 'LibreLight SDL-FIX-LIST '
  14. SHIFT_FINE = 0
  15. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  16. import tool.movewin as movewin
  17. import tool.git as git
  18. #CAPTION += git.get_all()
  19. win_title =CAPTION.strip().split()[-1]
  20. store = movewin.load_all_sdl(win_title)
  21. print(store)
  22. W=850
  23. H=460
  24. POS=[20,20]
  25. if store:
  26. W = store[-4]
  27. H = store[-3]
  28. POS=[store[-2],store[-1]]
  29. #exit()
  30. #CAPTION += ':{}'.format(random.randint(100,999))
  31. CAPTION += git.get_all()
  32. import pathlib
  33. _file_path=pathlib.Path(__file__)
  34. print("file:",_file_path)
  35. #_id = movewin.winfo(CAPTION)
  36. #c1 = movewin.movewin(_id,200,50)
  37. #os.system(c1)
  38. #c1 = movewin.activate(_id)
  39. #os.system(c1)
  40. movewin.check_is_started(CAPTION,_file_path)
  41. # ===== GUI =========
  42. import pygame
  43. import pygame.gfxdraw
  44. import pygame.font
  45. pg = pygame
  46. main_size=(W,H)#850,460)
  47. window = pygame.display.set_mode(main_size,pg.RESIZABLE,32)
  48. pg = pygame
  49. pygame.init()
  50. pygame.mixer.quit()
  51. clock = pygame.time.Clock()
  52. try:
  53. icon = pygame.image.load('icon/scribble.png')
  54. pygame.display.set_icon(icon)
  55. except Exception as e:
  56. print(" ERROR:",os.getcwd())
  57. print(" ERROR:set_icon ",e) #,color="red")
  58. import tool.movewin as movewin
  59. import tool.sdl_elm as sdl_elm
  60. pg.display.set_caption(CAPTION)
  61. font0 = pygame.font.SysFont("freesans",10)
  62. font0b = pygame.font.SysFont("freesansbold",10)
  63. font = pygame.font.SysFont("freemonobold",22)
  64. font10 = pygame.font.SysFont("freemonobold",10)
  65. font12 = pygame.font.SysFont("freemonobold",12)
  66. font15 = pygame.font.SysFont("freemonobold",15)
  67. font16 = pygame.font.SysFont("freemonobold",16)
  68. font18 = pygame.font.SysFont("freemonobold",18)
  69. font22 = pygame.font.SysFont("FreeSans",22)
  70. #font = pygame.font.SysFont(None,30)
  71. fr = font.render("hallo" ,1, (200,0,255))
  72. mouse_down = 0
  73. mouse_pos1 = [0,0]
  74. mouse_pos2 = [0,0]
  75. mouse_grab = []
  76. print(int((time.time()-boot)*10),"loop...")
  77. fps_t = time.time()
  78. fps = 0
  79. fps_old = 0
  80. import memcache
  81. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  82. mc.set("some_key", "Some value")
  83. value = mc.get("some_key")
  84. mc.set("another_key", 3)
  85. mc.delete("another_key")
  86. import time
  87. import json
  88. data = {}
  89. start = time.time()
  90. delta = start
  91. #for i in dir(mc):
  92. # print(i)#,[i.__doc__])
  93. # print()
  94. #for i in mc.get_stats():
  95. # print("keys",i)
  96. #fps_btn = []
  97. #fps_btn_press = []
  98. #i += 1
  99. #bx = sdl_elm.Button(window,pos=[30,r,190,60])
  100. #bx.text = "FIX:{}\n<val>\nx".format(i+1)
  101. #bx.font0 = pygame.font.SysFont("freesans-bold",20)
  102. #bx.btn1['type'] = "flash"
  103. #fps_btn.append(bx)
  104. #r+=bx.get_rect()[3]
  105. def read_dmx():
  106. dmx = {} #univ
  107. iii = 0
  108. y=mc.get("index")
  109. if y:
  110. key=y.keys()
  111. key = list(key)
  112. key.sort()
  113. for k in key:
  114. if k.startswith("ltp-out"):
  115. #v = y[k]
  116. u = k.split(":")[-1]
  117. x=mc.get(k)
  118. dmx[u] = x
  119. return dmx
  120. def read_fix(dmx):
  121. global mc
  122. y=mc.get("fix")#cmd)
  123. if y is None:
  124. print("==== "*10)
  125. print("error -- read_fix(dmx) mc.get('fix') return",y)
  126. print()
  127. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  128. return {}
  129. key=y.keys()
  130. key = list(key)
  131. key.sort()
  132. for k in key:#y.items():
  133. v = y[k]
  134. #print(k,v)
  135. x=mc.get(k)
  136. dmx_start = 0
  137. if "DMX" in v:
  138. dmx_start = v["DMX"]
  139. univ_start = 0
  140. if "UNIVERS" in v:
  141. univ_start = v["UNIVERS"]
  142. if "ATTRIBUT" in v: # and 10:
  143. ATTR = v["ATTRIBUT"]
  144. for k2 in ATTR:
  145. k2_ATTR = ATTR[k2]
  146. k2_ATTR["VALUE2"] = -2
  147. if is_hidden_attr(k2):
  148. #if k2.startswith("_"):
  149. continue
  150. k3 = k+"-"+k2
  151. dmx_nr = 0
  152. if "NR" in k2_ATTR:
  153. if k2_ATTR["NR"] >= 1:
  154. dmx_nr = k2_ATTR["NR"]+1
  155. val2 = ""
  156. if "VALUE" in k2_ATTR:
  157. val2 = k2_ATTR["VALUE"]
  158. dmx_val=-1
  159. dmx_x=-1
  160. if dmx_nr > 0 and dmx_start > 0:
  161. try:
  162. dmx_x = dmx_start-1+dmx_nr-1
  163. dmx_val = dmx[str(univ_start)][dmx_x-1]
  164. except:pass
  165. if type(dmx_val) in [int,float]:
  166. k2_ATTR["VALUE2"] = dmx_val
  167. else:
  168. k2_ATTR["VALUE2"] = 0
  169. return y
  170. def add_dmx(data,dmx):
  171. pass
  172. def reorder_table_by_pos(table_grid):
  173. if type(table_grid) in (dict,OrderedDict):
  174. # order table of type dict by pos [x,y]
  175. table_grid5 = {}
  176. for t5 in table_grid:
  177. t5_row = table_grid[t5]
  178. #print(t5,t5_row.pos)
  179. k5 = "{:010}-{:010}".format(t5_row.pos[0],t5_row.pos[1])
  180. table_grid5[k5] = [t5,t5_row]
  181. ordered = list(table_grid5.keys())
  182. ordered.sort()
  183. table_grid2 = OrderedDict()
  184. for k5 in ordered: #table_grid5:
  185. k5_row = table_grid5[k5]
  186. table_grid2[k5_row[0]] = k5_row[1]
  187. return table_grid2
  188. if type(table_grid) is list:
  189. # order table of type list by pos [x,y]
  190. table_grid5 = {}
  191. for t5 in table_grid:
  192. t5_row = t5 #table_grid[t5]
  193. #print(t5)#,t5_row.pos)
  194. k5 = "{:010}-{:010}".format(t5_row.pos[1],t5_row.pos[0])
  195. table_grid5[k5] = [t5,t5_row]
  196. ordered = list(table_grid5.keys())
  197. ordered.sort()
  198. table_grid2 = [] #OrderedDict()
  199. for k5 in ordered: #table_grid5:
  200. k5_row = table_grid5[k5]
  201. table_grid2.append( k5_row[1] )
  202. return table_grid2
  203. return table_grid
  204. def is_active_fix(v):
  205. if "ATTRIBUT" in v:
  206. if "_ACTIVE" in v["ATTRIBUT"]:
  207. if "ACTIVE" in v["ATTRIBUT"]["_ACTIVE"]:
  208. if v["ATTRIBUT"]["_ACTIVE"]["ACTIVE"] >=1:
  209. return 1
  210. return 0
  211. def is_hidden_attr(attr_name):
  212. if attr_name.endswith("-FINE"):
  213. return 1
  214. if attr_name.startswith("_"):
  215. return 1
  216. return 0
  217. def count_active_attr(fix_row):
  218. return len(get_active_attr(fix_row))
  219. def get_active_attr(fix_row):
  220. # fix_row = {ID:"12"NAME:"xyz",ATTRIBUTE:{"RED":{"VALUE":0}}}
  221. active_attr = []
  222. if "ATTRIBUT" in fix_row:
  223. attr_row = fix_row["ATTRIBUT"]
  224. for attr_name in attr_row:
  225. if is_hidden_attr(attr_name):
  226. continue
  227. attr_data = attr_row[attr_name]
  228. if "ACTIVE" in attr_data:
  229. if attr_data["ACTIVE"] >=1:
  230. active_attr.append(attr_name)
  231. return active_attr
  232. def get_attr_count(attr_row):
  233. acount = 0
  234. for attr in attr_row:
  235. if is_hidden_attr(attr):
  236. continue
  237. acount+=1
  238. return acount
  239. def get_fix_type(fix_row):
  240. attr2 = []
  241. if "ATTRIBUT" in fix_row:
  242. attr_row = fix_row["ATTRIBUT"]
  243. for attr in attr_row:
  244. if is_hidden_attr(attr):
  245. continue
  246. attr2.append(attr)
  247. if "PAN" in attr2 and "TILT" in attr2:
  248. return "MOVER"
  249. if "RED" in attr2 and "GREEN" in attr2 and "BLUE" in attr2:
  250. return "RGB"
  251. if len(attr2) == 1 and "DIM" in attr2:
  252. return "DIM"
  253. return "UNKNOWN"
  254. win_con = movewin.Control()
  255. win_con.title = win_title
  256. win_con.winfo()
  257. if POS:
  258. win_con.move(POS[0],POS[1])
  259. print(POS,win_con.title)
  260. table={}
  261. table_grid={}
  262. btn1_press = [] #["10.10.10.13:0"]
  263. y=[]
  264. bx_font0 = pygame.font.SysFont("freesans-bold",20)
  265. import lib.zchat as chat
  266. cmd_client = chat.Client(port=30003)
  267. err = []
  268. #err.append([time.time(),"init"])
  269. scroll_bar = sdl_elm.Button(window,pos=[640,40,40,400])
  270. scroll_bar.btn1.color_on = [255,255,0]
  271. scroll_bar.dbg = 0
  272. scroll_bar.btn4.color_on=[0,0,0]
  273. scroll_bar.btn4.color_off=[0,0,0]
  274. scroll_bar.fader = "v"
  275. scroll_bar.text="\n"*10+"<ival%>%"
  276. scroll_bar.text=" "
  277. scroll_bar.btn4.nr_on = [5]
  278. scroll_bar.btn4.nr_off = [4]
  279. scroll_bar.draw()
  280. width,hight = main_size #[1]
  281. scroll_max = 100
  282. def draw_frame(window):
  283. fr = font22.render("FIXTURE LIST " ,1, (200,200,200))
  284. window.blit(fr,(20,5 ))
  285. fr = font22.render("DEMO / TEST - MODE ! " ,1, (200,200,200))
  286. #window.blit(fr,(10,30 ))
  287. # -------------------
  288. x=200
  289. y=5
  290. bx = sdl_elm.Button(window,pos=[x,y,50,20])
  291. bx.text = " HELP "
  292. import lib.libtk as libtk
  293. def xhelp(event=None):
  294. #print(event)
  295. libtk.online_help("librelight:20-exec")()
  296. bx.btn1.cb_on.set(xhelp)
  297. #bx.draw()
  298. btn_help=bx
  299. # -------------------
  300. while 1:
  301. try:
  302. fps +=1
  303. t = time.time()
  304. if t-fps_t >= 1:
  305. #print("FPS:",fps)
  306. fps_old = fps
  307. fps=0
  308. fps_t =t
  309. pygame.display.flip()
  310. pos = [160,10,70,60]
  311. rgb = (0xdd,0xdd,0xdd,0)
  312. rgb = (0xaa,0xaa,0xaa,0)
  313. #window.fill((2,2,2))
  314. window.fill((0,0,0))
  315. pygame.draw.rect(window,(0,0,0),[0,0,main_size[0],main_size[1]])
  316. draw_frame(window)
  317. pos = [160,110,70+80,20]
  318. #pygame.draw.rect(window,rgb,pos)
  319. t=(time.time()-start)
  320. if t > 15:
  321. start = time.time()
  322. b= 80-int(t*10)
  323. pos = [160,110,70+(b),20]
  324. rgb = (0x00,0xff,0xff,0)
  325. dmx = read_dmx()
  326. rgb = (0xff,0,0xaa,0)
  327. data = read_fix(dmx)
  328. BTN_WIDTH = 120
  329. #scroll_bar.btn4.val._max = len(data)-8 #draw()
  330. scroll_max = len(data)-8
  331. scroll_max = len(data.keys())-8
  332. scroll_max = 1
  333. block_wrap = int( (width-200) / BTN_WIDTH)
  334. if block_wrap <= 0:
  335. block_wrap = 1
  336. for k in data.keys():
  337. fix_type = get_fix_type(data[k])
  338. if "ATTRIBUT" in data[k]:
  339. row = data[k]["ATTRIBUT"]
  340. acount = get_attr_count(row)
  341. if fix_type == "DIM":
  342. continue
  343. if acount > block_wrap:
  344. scroll_max += int(acount/block_wrap)
  345. else:
  346. scroll_max += 1
  347. #print()
  348. scroll_bar.btn4.val._max = scroll_max
  349. scroll_bar.increment = (len(data))/100*10 #draw()
  350. #data = add_dmx(data,dmx)
  351. scroll_pos = scroll_bar.btn4.val.get()
  352. scroll_bar.pos[0] = width-scroll_bar.pos[2]-5
  353. scroll_bar.pos[3] = hight-80
  354. #print(scroll_pos)
  355. table_grid_draw=[] #{}
  356. table_draw = []
  357. active_fix = 0
  358. active_attr = 0
  359. i = 0
  360. r=40
  361. if 1:
  362. ch = 141
  363. send = 0
  364. #cmd="stats items"
  365. #if not y:
  366. y=data #mc.get("fix")#cmd)
  367. if y:
  368. iii = 0
  369. key=y.keys()
  370. key = list(key)
  371. key2 = []
  372. for k in key:
  373. #print(k)
  374. try:
  375. key2.append(int(k))
  376. except:
  377. pass
  378. key2.sort()
  379. key = key2 #.sort()
  380. if len(btn1_press) == 0:
  381. btn1_press = [key[0]]
  382. rgb = (0x00,0,0xff,0)
  383. k2 = btn1_press[-1]
  384. #fr = font22.render("SRC:"+str(k2) ,1, rgb) #(200,200,200))
  385. fr = font15.render("FPS:"+str(fps_old) ,1, rgb) #(200,200,200))
  386. window.blit(fr,(600,5))
  387. err_r = 0
  388. if err:
  389. for e in err:
  390. rgb = (255,0,0)
  391. fr = font15.render("err:"+str(e) ,1, rgb) #(200,200,200))
  392. window.blit(fr,(700,5+err_r))
  393. err_r += 20
  394. i4 = 0
  395. for k in key:
  396. k = str(k)
  397. fix_row = y[k]
  398. fix_type = get_fix_type(fix_row)
  399. if fix_type == "DIM":
  400. continue
  401. active_fix += is_active_fix(fix_row)
  402. active_attr += count_active_attr(fix_row)
  403. for k in key:#y.items():
  404. k = str(k)
  405. v = y[k]
  406. fix_row = v
  407. fix_type = get_fix_type(fix_row)
  408. if fix_type == "DIM":
  409. continue
  410. #scroll_max+=1
  411. i4 += 1
  412. if i4 < scroll_pos:
  413. continue
  414. #print(k,v)
  415. x=mc.get(k)
  416. cccount = 0
  417. txt = str([k,v,ch,"=",cccount]) #x[ch-1]])
  418. rgb = (0xaa,0xaa,0xaa,0)
  419. i += 1
  420. if k not in table:
  421. bx = sdl_elm.Button(window,pos=[20+20,r,50,20])
  422. bx.btn1.color_on = [255,255,0]
  423. bx.ID = -1
  424. if "ID" in v:
  425. bx.ID = v["ID"]
  426. if bx.ID == 0:
  427. bx.ID = -2
  428. bx.ATTR = "_ACTIVE"
  429. table[k] = bx
  430. # color box
  431. bxc = sdl_elm.Button(window,pos=[-11+20,r,5,20])
  432. bxc.btn1.color_on = [255,255,0]
  433. table[k+"_color"] = bxc
  434. bx = table[k]
  435. bx.data = v
  436. bxc.data = {}
  437. active = is_active_fix(v)
  438. bx.btn1.val.set(active)
  439. try:
  440. if int(k) % 10 == 0:
  441. #pygame.draw.aaline(window,[255,0,0],[2,r],[800,r],1)
  442. pygame.draw.rect(window,[58,58,58],[2,r,2000,25])
  443. except:pass
  444. bx.text = "ID:"+ k
  445. bx.text += " "+v["NAME"]
  446. bx.font0 = bx_font0
  447. bx.btn1.bg_on = [0,255,255]
  448. bx.btn1.type = "toggle"
  449. bx.pos = [10+10,r,BTN_WIDTH,20]
  450. if "ATTRIBUT" in v:
  451. bcv_r = 0
  452. bcv_g = 0
  453. bcv_b = 0
  454. if "RED" in v["ATTRIBUT"]:
  455. bcv_r = v["ATTRIBUT"]["RED"]["VALUE2"]
  456. if "GREEN" in v["ATTRIBUT"]:
  457. bcv_g = v["ATTRIBUT"]["GREEN"]["VALUE2"]
  458. if "BLUE" in v["ATTRIBUT"]:
  459. bcv_b = v["ATTRIBUT"]["BLUE"]["VALUE2"]
  460. #print("bvc_rgb" [bcv_r,bcv_g,bcv_b])
  461. if bcv_r > 255:
  462. bcv_r=255
  463. if bcv_g > 255:
  464. bcv_g=255
  465. if bcv_b > 255:
  466. bcv_b=255
  467. bxc.btn1.color = [bcv_r,bcv_g,bcv_b]
  468. bxc.btn1.color_on = [bcv_r,bcv_g,bcv_b]
  469. bxc.pos = [140+5,r,20,20]
  470. bxc.text = ""
  471. bx.draw()
  472. bxc.draw()
  473. r_buf=bx.get_rect()[3]+2
  474. iii += 35
  475. rr = 0
  476. acount = 0
  477. if "ATTRIBUT" in v: # and 10:
  478. ATTR = v["ATTRIBUT"]
  479. for k2 in ATTR:
  480. k2_ATTR = ATTR[k2]
  481. if is_hidden_attr(k2):
  482. continue
  483. k3 = k+"-"+k2
  484. val2 = ""
  485. if "VALUE" in k2_ATTR:
  486. val2 = k2_ATTR["VALUE"]
  487. virt = 1
  488. if "NR" in k2_ATTR:
  489. if k2_ATTR["NR"] > 0:
  490. virt = 0
  491. dmx_val = -3
  492. if "VALUE2"in k2_ATTR:
  493. dmx_val = k2_ATTR["VALUE2"]
  494. if k3 not in table_grid:
  495. bx = sdl_elm.Button(window,pos=[600,rr,60,20])
  496. bx.btn1.color_on = [255,255,0]
  497. bx.ID = 0
  498. if "ID" in v:
  499. bx.ID = v["ID"]
  500. bx.ATTR = k2
  501. table_grid[k3] = bx
  502. if "ACTIVE" in k2_ATTR:
  503. if k2_ATTR["ACTIVE"] >=1:
  504. table_grid[k3].btn1.val.set(1)
  505. #active_attr += 1
  506. else:
  507. table_grid[k3].btn1.val.set(0)
  508. bx = table_grid[k3]
  509. bx.data = k2_ATTR
  510. try:val = v
  511. except:pass
  512. bx.text = k2 +" "+str(val2)+" "+str(dmx_val)
  513. bx.font0 = bx_font0
  514. if k2 == "RED":
  515. bx.btn4.color_on = [255,0,0]
  516. elif k2 == "GREEN":
  517. bx.btn4.color_on = [0,255,0]
  518. elif k2 == "BLUE":
  519. bx.btn4.color_on = [0,0,255]
  520. if virt == 1:
  521. bx.btn3.color= [0,0,0]
  522. bx.btn1.type = "toggle"
  523. if type(dmx_val) == int:
  524. bx.btn4.val.set(dmx_val) # "toggle"
  525. bx.pos = [170+rr,r,BTN_WIDTH,20]
  526. bx.draw()
  527. rr+=bx.get_rect()[2]+2
  528. table_grid_draw.append(k3) #table_grid[k3]
  529. if rr > 1000:
  530. break
  531. acount +=1
  532. if acount %block_wrap==0:
  533. r += r_buf
  534. rr = 0
  535. table_draw.append(k)
  536. r += r_buf
  537. if r > hight-30:#/20:
  538. break
  539. active_ratio = 0
  540. if active_fix:
  541. active_ratio = (active_attr/active_fix)
  542. fr = font18.render("ACTIVE:{}:{} ({:0.01f})".format(active_fix,active_attr,active_ratio) ,1, [255,255,0]) #(200,200,200))
  543. window.blit(fr,(300,5))
  544. if SHIFT_FINE:
  545. fr = font18.render("ENCODER:±0.25" ,1, [255,255,0])
  546. else:
  547. fr = font18.render("ENCODER:±5.00" ,1, [255,255,0])
  548. window.blit(fr,(420,5))
  549. if 0: #show line number +scroll_pos
  550. bxc = sdl_elm.Button(window,pos=[-11+10,(0*23),50,10])
  551. bxc.text = "{:0.02f} {}".format(scroll_pos,scroll_max)
  552. ii = int(scroll_pos) #int(scroll_pos/23*29)
  553. bxc.font0 = bx_font0
  554. bxc.draw()
  555. for i in range(40+1):
  556. bxc = sdl_elm.Button(window,pos=[0,40+(i*23),30,10])
  557. bxc.text = "{}".format(1+i+ii)
  558. bxc.btn1.color = [255,200,200]
  559. bxc.btn2.color = [255,200,200]
  560. bxc.btn3.color = [255,200,200]
  561. bxc.font0 = bx_font0
  562. bxc.draw()
  563. btn_help.draw()
  564. resize_changed = 0
  565. for raw_event in pygame.event.get():
  566. x_change=btn_help.event(raw_event)
  567. event = {'unicode': '', 'key': 0, 'mod': 0, 'scancode': 0, 'window': None,'type':'','button':''}
  568. event['type'] = raw_event.type
  569. event.update( raw_event.dict)
  570. if event['button'] and event["type"] in [1025,1026]:
  571. event["type"] -= 1020
  572. if event["button"] or event['scancode']:
  573. print(" event:",event)
  574. if "scancode" in event:
  575. #print(" ",event)
  576. if event['scancode'] in [50,225]: # shift old/new
  577. if event['type'] in [2,768]: # press
  578. #pg.display.set_caption(CAPTION+ " SHIFT/FINE")
  579. SHIFT_FINE = 1
  580. if event['type'] in [3,769]: # release
  581. #pg.display.set_caption(CAPTION)
  582. SHIFT_FINE = 0
  583. if event['scancode'] in [9,41]:
  584. for k in table_draw:
  585. t = table[k]
  586. t.btn1.clean()
  587. for k in table_grid_draw:
  588. t = table_grid[k]
  589. t.btn1.clean()
  590. msg=json.dumps([{"event":"ESC"}]).encode("utf-8")
  591. print("ESC",msg)
  592. cmd_client.send(msg)
  593. if event['mod'] == 0:
  594. keycode = {27:"REC",39:"SELECT",46:"LABEL",54:"CFG-BTN",56:"BLIND",41:"FLASH",26:"EDIT"}
  595. #print(" 45:", event['scancode'] in keycode,event['scancode'])
  596. if event['scancode'] in keycode: # r
  597. if event['type'] == 2: # press
  598. msg=json.dumps([{"event":keycode[event['scancode']]}]).encode("utf-8")
  599. print("SPCIAL-KEY",msg)
  600. cmd_client.send(msg)
  601. if event['mod'] == 64:
  602. keycode = {39:"SAVE\nSHOW",54:"RESTART"}
  603. print( " 56:",event['scancode'] in keycode,event['scancode'])
  604. if event['scancode'] in keycode: # r
  605. if event['type'] == 2: # press
  606. msg=json.dumps([{"event":keycode[event['scancode']]}]).encode("utf-8")
  607. print("SPCIAL-KEY",msg)
  608. cmd_client.send(msg)
  609. if event['scancode'] in range(10,20+1) or event['scancode'] in range(30,39+1):
  610. if event['type'] in [2,3,768,769]: # press
  611. if event['type'] in [2,769]:
  612. v=0
  613. if event['type'] in [3,768]:
  614. v=255
  615. btn_nr=-3000
  616. if event['scancode'] in range(10,20+1):
  617. btn_nr = event['scancode']-10+1
  618. if event['scancode'] in range(30,39+1):
  619. btn_nr = event['scancode']-30+1
  620. btn_nr_raw = btn_nr
  621. btn_nr += 161-1
  622. msg=json.dumps([{"event":"EXEC","EXEC":btn_nr,"VAL":v,"NR-KEY":btn_nr_raw}]).encode("utf-8")
  623. print("SPCIAL-KEY",msg)
  624. cmd_client.send(msg)
  625. if event['scancode'] in range(67,76+1) or event['scancode'] in [95,96]:
  626. if event['type'] in [2,3,768,769]: # press
  627. if event['type'] in [2,768]:
  628. v=0
  629. if event['type'] in [3,769]:
  630. v=255
  631. btn_nr = event['scancode']
  632. if btn_nr == 95:
  633. btn_nr = 11
  634. elif btn_nr == 96:
  635. btn_nr = 12
  636. else:
  637. btn_nr = event['scancode']-66
  638. btn_nr_raw = btn_nr
  639. btn_nr += 81-1
  640. msg=json.dumps([{"event":"EXEC","EXEC":btn_nr,"VAL":v,"F-KEY":btn_nr_raw}]).encode("utf-8")
  641. print("SPCIAL-KEY",msg)
  642. cmd_client.send(msg)
  643. if event['type'] == pygame.QUIT:
  644. print()
  645. print("quit",event)
  646. pygame.quit()
  647. sys.exit(0)
  648. elif event['type'] == pygame.VIDEORESIZE:
  649. print()
  650. print("resize",event)
  651. scrsize = event['size']
  652. width = event['w']
  653. hight = event['h']
  654. resize_changed = True
  655. scroll_bar.event(raw_event) #daraw()
  656. scroll_change = 0
  657. if scroll_bar.btn3.val.get(): #scroll_bar focus
  658. spos = scroll_bar.rel_pos[1]
  659. #print("------------------",spos)
  660. if "button" in event:
  661. if event["button"] == 1:
  662. scroll_bar.btn4.val.set(scroll_bar.btn4.val._max*spos)
  663. if "buttons" in event:
  664. if event["buttons"][0]:
  665. scroll_bar.btn4.val.set(scroll_bar.btn4.val._max*spos)
  666. event_lock = scroll_bar.btn3.val.get() #focus on
  667. if not event_lock:
  668. for t in table_draw:
  669. table[t].event(raw_event)
  670. if table[t].btn3.get():
  671. data = table[t].data
  672. if not event_lock:
  673. for k3 in table_draw:
  674. #print(t)
  675. row = table[k3]
  676. change = table[k3].event(raw_event)
  677. if row.btn3.get():
  678. # FIXTURE SELECTOR
  679. data = row.data
  680. FIX = row.ID
  681. ATTR = row.ATTR
  682. key = "BUTTON"
  683. if key in change:
  684. if "press" in change[key]:
  685. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"click","ATTR":ATTR}]).encode("utf-8")
  686. print(" msg:",msg)
  687. cmd_client.send(msg)
  688. if "release" in change[key]:
  689. pass
  690. if not event_lock:
  691. for k3 in table_grid_draw:
  692. row = table_grid[k3]
  693. change = table_grid[k3].event(raw_event)
  694. if row.btn3.get():
  695. data = row.data
  696. FIX = row.ID
  697. ATTR = row.ATTR
  698. key = "MOUSE ENCODER"
  699. if key in change:
  700. ACC = 2
  701. if SHIFT_FINE:
  702. ACC = 1
  703. VAL = ""
  704. if "press" in change[key]:
  705. VAL = "+"*ACC
  706. if "release" in change[key]:
  707. VAL = "-"*ACC
  708. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":VAL,"ATTR":ATTR}]).encode("utf-8")
  709. print(" ",msg)
  710. cmd_client.send(msg)
  711. key = "BUTTON"
  712. if key in change:
  713. if "press" in change[key]:
  714. #print(" ATTR:",FIX,ATTR,data)
  715. #print(" CHANGE",change)
  716. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"click","ATTR":ATTR}]).encode("utf-8")
  717. print(" ",msg)
  718. cmd_client.send(msg)
  719. if "release" in change[key]:
  720. pass
  721. if "pos" in event:
  722. if "button" in event:
  723. if event['type'] in [5,1025]:#press
  724. mouse_down = 1
  725. mouse_pos1 = [event['pos'][0],event['pos'][1]]
  726. if event['type'] in [6,1026]:#release
  727. mouse_down = 0
  728. mouse_pos2 = [event['pos'][0],event['pos'][1]]
  729. if event_lock:
  730. pass
  731. elif "button" in event:
  732. if event['type'] in [6,1026]:
  733. if event["button"] == 1:
  734. mouse_grab_active = 0
  735. for mg in mouse_grab:
  736. if mg.btn1.val.get():
  737. mouse_grab_active += 1
  738. for mg in mouse_grab:
  739. FIX = str(mg.ID)
  740. ATTR = str(mg.ATTR)
  741. if mouse_grab_active:
  742. if not mg.btn1.val.get():
  743. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"click","ATTR":ATTR}]).encode("utf-8")
  744. print(" send:",msg)
  745. cmd_client.send(msg)
  746. else: #no btn is on
  747. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS","FIX":str(FIX),"VAL":"click","ATTR":ATTR}]).encode("utf-8")
  748. print(" send:",msg)
  749. cmd_client.send(msg)
  750. mouse_grab = []
  751. if event["button"] == 3:
  752. mouse_grab_active = 0
  753. for mg in mouse_grab:
  754. if mg.btn1.val.get():
  755. mouse_grab_active += 1
  756. for mg in mouse_grab:
  757. FIX = str(mg.ID)
  758. ATTR = str(mg.ATTR)
  759. if mouse_grab_active:
  760. if mg.btn1.val.get():
  761. msg = json.dumps([{"event":"FIXTURES","TYPE":"ENCODERS"
  762. ,"FIX":str(FIX),"VAL":"click"
  763. ,"ATTR":ATTR}]).encode("utf-8")
  764. #print(" mouse_grab ",msg,mg.btn1.val.get())
  765. cmd_client.send(msg)
  766. mouse_grab = []
  767. if mouse_down:
  768. d1 = mouse_pos1[0]-mouse_pos2[0]
  769. d2 = mouse_pos1[1]-mouse_pos2[1]
  770. pix = 10
  771. if ( d1 > pix or d1 < -pix) or ( d2 >pix or d2 < -pix):
  772. sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
  773. for k in table_draw: # FIX-ID
  774. t = table[k]
  775. pos = t.get_rect()
  776. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  777. if sdl_elm.check_area2(pos,mpos):
  778. if t not in mouse_grab:
  779. mouse_grab.append(t)
  780. else:
  781. if t in mouse_grab:
  782. mouse_grab.remove(t)
  783. for k3 in table_grid_draw: # FIX-ATTR
  784. t = table_grid[k3]
  785. pos = t.get_rect()
  786. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  787. if sdl_elm.check_area2(pos,mpos):
  788. if t not in mouse_grab:
  789. mouse_grab.append(t)
  790. else:
  791. if t in mouse_grab:
  792. mouse_grab.remove(t)
  793. for k3 in table_grid:
  794. t = table_grid[k3]
  795. t._set_mouse_focus(0)
  796. i = 1
  797. if mouse_grab:
  798. mouse_grab = reorder_table_by_pos(mouse_grab)
  799. for t in mouse_grab:
  800. t._set_mouse_focus(1)
  801. pos = t.pos[:2]
  802. pos[0] += 100
  803. pos[1] += 8
  804. rgb = (0,255,255)
  805. fr = font15.render(""+str(i) ,1, rgb) #(200,200,200))
  806. #print(pos)
  807. pygame.draw.rect(window,(0,0,0),[pos[0]-2,pos[1]-2,15,13])
  808. window.blit(fr,pos)
  809. i+=1
  810. if resize_changed:# = True
  811. screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
  812. scroll_bar.draw()
  813. clock.tick(6)
  814. except Exception as e:
  815. err.append([int((time.time()-boot)*100),e])
  816. traceback.print_exc()
  817. print("Exc",e)