Browse Source

cleanup Ray-Fix

micha 1 day ago
parent
commit
a8908a1f0d
1 changed files with 118 additions and 62 deletions
  1. 118 62
      tkray/fix.py

+ 118 - 62
tkray/fix.py

@@ -107,7 +107,8 @@ class BUTTON():
 
         self.btn.btn1.name=txt
         #x=btn.btn1.name
-
+    def __repr__(self):
+        return "<BUTTON {} {} {}>".format(id(self),str(self.btn.pos),self.btn.btn1.name)
     def draw(self,event=None):
         font_size = font10.baseSize
         pos=self.btn.pos
@@ -225,13 +226,101 @@ def check_area(pos,event_pos):
     if x and y:
         return 1
 
+_load_fix_DATA = {}
+_load_fix_host_list = {}
+_load_fix_last = 0
+_load_fix_keys = 0
+_load_fix_init = 0
+_load_fix_keys = []
+def load_fix():
+    global _load_fix_DATA 
+    global _load_fix_host_list 
+    global _load_fix_keys 
+    global _load_fix_init
+
+    if _load_fix_last+0.1 < time.time() and _load_fix_init==1:
+        return _load_fix_keys.copy(), _load_fix_host_list.copy()
+    
+    _last = time.time()
+    _load_fix_init = 1
+
+    DATA=mc.get("fix")#cmd)
+    keys = []
+    host_list = {}
+    if not DATA:
+        DATA = {}
+        print("mc.get ist NONE")
+        #continue
+
+    for k in DATA:
+        keys.append(k)
+        host_list[k] = DATA[k] #[0]).ljust(" ",18) #,0,"=",0]
+    keys.sort()
+
+    _load_fix_keys = keys
+    _load_fix_host_list = host_list 
+
+    return _load_fix_keys.copy(), _load_fix_host_list.copy()
+
+XFIRST_RUN = 1
+def check_buttons(buttons,host_list):
+    global XFIRST_RUN 
+    _x=30
+    _y=60
+    if not XFIRST_RUN:
+        return
+    XFIRST_RUN = 0
+
+    if 1:#FIRST_RUN:
+        for i,k in enumerate(host_list): #.items():
+            key = str(i)
+            if key not in buttons:
+                v=host_list[k]
+                txt=v["NAME"]
+                btn=BUTTON(None,txt,[_y,_x,30,60])
+                btn.btn.btn1.color_on = [255,255,0,255]
+                buttons[key] = btn
+                #print("ADD",key,btn)
+
+                if "ATTRIBUT" in v:
+                    #print(txt)
+                    ATTR = v["ATTRIBUT"]
+                    if 1:
+                        ay2=_y
+                        ax2=_x+70
+                        for kk,vv in v["ATTRIBUT"].items():
+                            if "-FINE" in kk or kk.startswith("_"):
+                                continue
+                            key = str(i)+":" +kk
+                            VAL = vv["VALUE"]
+                            print("-",kk,vv)
+                            txt = kk+" "+str(VAL)
+                            btn=BUTTON(None,txt,[ay2,ax2,30,70])
+                            btn.btn.btn1.color_on = [255,255,0,255]
+                            buttons[key] = btn
+                            add=1
+                            ax2+=73
+
+            _y+=30
+
+def draw_buttons(buttons):
+    for k,btn in buttons.items():
+        btn.event(old_x,old_y,mouse_old)
+        #print(btn)
+        pos = btn.btn.pos
+        if pos[0] > 300:
+            continue
+        btn.draw()
 
 buttons={} #[]
 cur = None
 history = []
 KEY_BUF={}
-
+FIRST_RUN = 1
 add=0
+
+core_tick_t = time.time()
+
 while not pyray.window_should_close():
     pyray.begin_drawing()
 
@@ -243,68 +332,19 @@ while not pyray.window_should_close():
 
     font_size = font10.baseSize
     try:
-        DATA=mc.get("fix")#cmd)
-        GREY = [122,122,122,255]
-        p=0
-        keys = []
-        host_list = {}
-        if not DATA:
-            DATA = {}
-            print("mc.get ist NONE")
-            pyray.draw_text_ex(font25,"ERR:{NO DATA}",[10,5], 25, 0,  pyray.VIOLET)
-            pyray.end_drawing()
-            time.sleep(0.5)
-            continue
+        keys,host_list = load_fix()
 
-        for k in DATA:
-            #print(k,DATA[k])
-            #print(k)
-            keys.append(k)
-            host_list[k] = DATA[k] #[0]).ljust(" ",18) #,0,"=",0]
-        keys.sort()
+        GREY = [122,122,122,255]
         
         k=keys[0]
 
         if cur is None:
             cur = k #k[3] #"xx123" # k
 
+        check_buttons(buttons,host_list)
+
+
 
-        p=0
-        start = 0
-        end   = start+10
-        i=0
-        for k,v in host_list.items():
-            if i > end:
-                break
-            if i < start:
-                continue
-
-            x2 = 30
-            y2 = 60+p #grid_y+p
-            v = str(v).replace(" ","").replace("[","").replace("]","") 
-            txt = str(k)+" "+str(v)
-
-            key = txt.split(" ")[0]
-            p+=32
-            if key in buttons:
-                btn = buttons[key]
-            else:
-                btn=BUTTON(None,txt,[y2,x2,30,690])
-                buttons[key] = btn
-                print("ADD",key,btn)
-                add=1
-            btn.btn.btn1.name = txt
-            btn.btn.btn1.color_on = [255,255,0,255]
-            #btn.btn.btn1.val.set(0)
-            #if cur in btn.btn.btn1.name:
-            #    btn.btn.btn1.val.set(255)
-            #    #btn.btn.btn1.press() #val.set(255)
-            i+=1
-
-
-        if add:
-            add=0
-            print()
         w=200
         h=50
 
@@ -312,6 +352,8 @@ while not pyray.window_should_close():
         y=200
 
         pyray.draw_text_ex(font25,"FPS:{}".format(fps_count),[10,5], 25, 0,  pyray.VIOLET)
+        pyray.draw_text_ex(font25,"BTN:{}".format(len(buttons)),[150,5], 25, 0,  pyray.VIOLET)
+        pyray.draw_text_ex(font25,"FIX:{}".format(len(host_list)),[350,5], 25, 0,  pyray.VIOLET)
 
         m=pyray.get_mouse_position()
         #print(m)
@@ -357,12 +399,10 @@ while not pyray.window_should_close():
             #print(mouse)
             mouse_old = mouse[:]
             
-
-        for k,btn in buttons.items():
-            btn.event(old_x,old_y,mouse_old)
-            btn.draw()
+        draw_buttons(buttons)
         
-        pyray.draw_text_ex(font20,"POS:{}:{}".format(int(m.x),int(m.y)),[10,34], 20, 0,  pyray.VIOLET)
+        pyray.draw_text_ex(font20,"X:{}".format(int(m.x)),[10,34], 20, 0,  pyray.VIOLET)
+        pyray.draw_text_ex(font20,"Y:{}".format(int(m.y)),[65,34], 20, 0,  pyray.VIOLET)
 
         pyray.draw_rectangle(int(old_x-10),int(old_y-1),20,2,[255,0,255,255])
         pyray.draw_rectangle(int(old_x-1),int(old_y-10),2,20,[255,0,255,255])
@@ -371,7 +411,16 @@ while not pyray.window_should_close():
 
         pyray.end_drawing()
         
-        time.sleep(1/11)
+        # frame tick speed, CPU LOAD. ------
+        delta = 1/10
+        if delta > (time.time()-core_tick_t):
+            delta -= (time.time()-core_tick_t)
+        #print(delta)
+        time.sleep(delta)
+        core_tick_t = time.time()
+        # --------------------------------
+
+
 
         for i,v in enumerate(mouse_old):
             if v == "release":
@@ -389,8 +438,15 @@ while not pyray.window_should_close():
         #POS[0]+=1
         if pyray.is_window_resized():
             print( "RESIZE WINDOW OK")
+            #for i in dir(pyray):
+            #    if "height" in i.lower(): 
+            #        print(i)
+            w = pyray.get_screen_width()
+            h = pyray.get_screen_height()
+            print(h,w)
         #print(pyray.FLAG_WINDOW_RESIZABLE  )
         #print("E",pyray.is_window_focused() )
+        FIRST_RUN = 0
     except KeyboardInterrupt as e:
         raise e
     except Exception as e:# KeyInterupt