Browse Source

add: font to ray-dmx (raylib)

micha 11 months ago
parent
commit
4e495767de
3 changed files with 628 additions and 24 deletions
  1. 107 23
      tkray/dmx.py
  2. 2 1
      tool/sdl_elm.py
  3. 519 0
      tool/tk_elm.py

+ 107 - 23
tkray/dmx.py

@@ -1,5 +1,25 @@
 from pyray import *
-init_window(800, 450, "RAY-DMX")
+import pyray 
+ConfigFlags(FLAG_MSAA_4X_HINT) #|FLAG_WINDOW_RESIZABLE  )
+#ConfigFlags(FLAG_WINDOW_RESIZABLE  )
+ConfigFlags(FLAG_WINDOW_HIGHDPI )
+init_window(800, 450, "RAY-DMX",10,10,10,10)
+#pyray.TextureFilter(font1,1)
+
+import sys
+sys.path.insert(0,"/opt/LibreLight/Xdesk/")
+import tool.tk_elm as tk_elm
+
+img = "/opt/LibreLight/Xdesk/icon/scribble.png"
+IMG = load_image(img)
+print(set_window_icon(IMG)) 
+
+
+
+#SetWindowIcon(Image image) 
+#for d in dir():
+#    if "image" in d.lower():
+#        print(d)
 
 import time
 
@@ -11,12 +31,68 @@ mc = memcache.Client(['127.0.0.1:11211'], debug=0)
 
 old_x = -10
 old_y = -10
+start = time.time()
+frame_count = 0
+fps_count = 0
+
+#a = "resources/pixantiqua.ttf"
+#a = "/lib/firefox-esr/fonts/TwemojiMozilla.ttf"
+#a = "/lib/python3/dist-packages/pygame/freesansbold.ttf"
+#a = "/usr/share/fonts/truetype/freefont/FreeMono.ttf"
+#a = "/usr/share/fonts/truetype/freefont/FreeSerif.ttf"
+#a = "/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf"
+a = "/usr/share/fonts/truetype/freefont/FreeSans.ttf"
+#a = "/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf"
+
+
+k=200
+i= 40 #60
+#font1 = load_font_ex(a, i, pyray.ffi.new('int *', 6), 1024);
+font1 = load_font_ex(a, i, None, 0);
+#font1 = load_font(a ) #, i, pyray.ffi.new('int *', 2), k);
+
+# `SetTextureFilter(font.texture, TEXTURE_FILTER_TRILINEAR)` did the trick. 
+# `TEXTURE_FILTER_BILINEAR` also worked fine, but the trilinear option worked better. 
+
+pyray.TextureFilter(3)
 
 while not window_should_close():
+    begin_drawing()
+    for o in dir(font1):
+        print(o)
+    print()
+    print(font1.texture) # (3)
+
+    clear_background(BLACK)
+    Color(255,0,0,0)
+    rl_enable_smooth_lines()
+    if 0: # font test
+        draw_line(2, 2, 100, 2, (255,225,0,220))
+        print(load_font_ex.__doc__) #("resources/pixantiqua.ttf", 32, 0, 250);
+
+        i = 20
+        
+        draw_text_ex(font1,b"73qwertzuio", [30,12], 45, 0, YELLOW)# VIOLET)
+        draw_text_ex(font1,"11a", [37,212], 45, 0, YELLOW)# VIOLET)
+        draw_text_ex(font1,"a1131", [44,302], 45, 0, YELLOW)# VIOLET)
+
+        draw_text(str("{} {}".format(i,k)), 50, 75, 34, YELLOW)# VIOLET)
+
+    if 10:
+        btn = tk_elm.Button(None,pos=[350,12])
+        x=btn.btn1.name
+        a = btn.pos
+        draw_text(str(x), 5, a[0]   , a[1], YELLOW)# VIOLET)
+        x=btn.btn2.name
+
+        draw_text(str(x), 5, a[0]+15, a[1], YELLOW)# VIOLET)
+        x=btn.btn3.name
+        draw_text(str(x), 5, a[0]+30, a[1], YELLOW)# VIOLET)
+        x=btn.btn4.name
+
+    font_size = 14
     try:
         y=mc.get("index")#cmd)
-        begin_drawing()
-        clear_background(BLACK)
         GREY = [122,122,122,255]
         p=0
         keys = []
@@ -25,25 +101,25 @@ while not window_should_close():
         keys.sort()
 
         k=keys[2]
-        xi=0
-        yi=0
         for i,v in enumerate(range(20+1)):
+            # COL NUMBER -> 1 2... 20
+            x2 = 180+i*30
+            y2 =   5 #+i*13
             txt=str(i+1)
-            draw_text(txt, 180+xi*30, 5+yi*13, 11, YELLOW)# VIOLET)
-            xi+=1
-            if xi % 20 == 0:
+            #draw_text(txt, x2, y2, 11, YELLOW)
+            draw_text_ex(font1,txt, [x2,y2], font_size, 0, YELLOW)
+            i+=1
+            if i % 20 == 0:
                 break
         xi=0
         yi=0
 
-        #draw_text(str(1), 170+xi*30, 5+yi*13, 11, YELLOW)# VIOLET)
         for i,v in enumerate(mc.get(k)):
             #print(i,v)
             txt = str(i)+":"+str(v)
             txt = str(v)
             x2 = 180+xi*30
-            y2 =  25+yi*13
-
+            y2 =  25+yi*16
             try:
                 draw_rectangle(x2-2, y2-2,24,13,[255,255,255,int(v)])
             except:
@@ -52,30 +128,31 @@ while not window_should_close():
                 int(v)
             except:
                 v=0
+
             if int(v) > 100:
-                draw_text(txt, x2, y2, 11, BLACK)# VIOLET)
+                draw_text_ex(font1,txt, [x2,y2], font_size, 0, BLACK)# VIOLET)
             else:
-                draw_text(txt, x2, y2, 11, GREY)# VIOLET)
+                draw_text_ex(font1,txt, [x2,y2], font_size, 0, GREY)# VIOLET)
             xi+=1
 
             if xi % 20 == 0:
-                #for o in FontType:
-                #    print(o)
-                ##exit()
-                #draw_texture_pro(FontType.FONT_DEFAULT,"hi",[10,10],[0,0],0,10)
-                #draw_text_pro(font: Font, text: str, position: Vector2, origin: Vector2, rotation: float, fontSize: float, spacing: float, tint: Color)
-                draw_text(str(int(yi*20)+1), 170-30, y2, 11, YELLOW)# VIOLET)
+                txt = str(int(yi*20)+1)
+                draw_text_ex(font1,txt, [170-30,y2], font_size, 0, YELLOW)# VIOLET)
                 xi = 0
                 yi += 1
 
 
 
-        draw_text(str(int(yi*20)+1), 170-30, y2, 11, YELLOW)# VIOLET)
+        txt = str(int(yi*20)+1)
+        draw_text_ex(font1,txt, [170-30,y2], font_size, 0, YELLOW)# VIOLET)
         p=0
         for k in keys:
-            #print(k)
+            # HOST LIST  
+            x2 = 10
+            y2 =  20+p
             txt =":"+str(k) 
-            draw_text(txt, 10, 20+p, 20, GREY)# VIOLET)
+            #draw_text(txt, x2, y2, 20, GREY)# VIOLET)
+            draw_text_ex(font1,txt, [x2,y2], font_size, 0, YELLOW)# VIOLET)
             p+=20
 
         x=100
@@ -92,7 +169,7 @@ while not window_should_close():
         #for i in  range(0,512):
         #    if is_key_down(i):
         #        print(i)
-        #draw_text("Hello world", 190, 200, 20, VIOLET)
+        draw_text("FPS:{}".format(fps_count), 3, 3, 3, VIOLET)
         #Color(255,0,0,0)
         m=get_mouse_position()
         if m.x != old_x or m.y != old_y:
@@ -103,6 +180,13 @@ while not window_should_close():
         draw_rectangle(int(old_x-1),int(old_y-10),2,20,[255,0,255,255])
         end_drawing()
         time.sleep(0.1)
+
+        frame_count += 1
+        if time.time()-start > 1:
+            start = time.time()
+            fps_count = frame_count
+            frame_count = 0
+
     except KeyboardInterrupt as e:
         raise e
     except Exception as e:# KeyInterupt

+ 2 - 1
tool/sdl_elm.py

@@ -296,7 +296,8 @@ class Button():
         self.text2 = []
 
     def __repr__(self):
-        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))
+        x="<sdl.BUTTON name:{} ID:{}-{:8} btn1:{:03} val2:{:03} id at {}>"
+        x=x.format(self.btn1.name,self.ID,self.ATTR,self.btn1.val.get(),self.btn4.val.get(), id(self))
         return x
     def check(self):
         if self.dbg:

+ 519 - 0
tool/tk_elm.py

@@ -0,0 +1,519 @@
+#!/usr/bin/python3 
+from lib.xcolor import *
+
+font0 = ("freesans",10)
+
+font0b = ("freesansbold",10)
+font = ("freemonobold",22)
+font10 = ("freemonobold",10)
+font12 = ("freemonobold",12)
+font15 = ("freemonobold",15)
+font22 = ("FreeSans",22)
+#font = (None,30)
+
+def check_rgb(rgb):
+    rgb_out = [127,127,127,127]
+    try:    
+        for i,v in enumerate(rgb):
+            if v > 255:
+                v = 255
+            if v < 0:
+                v = 0
+            rgb_out[i] = v
+
+    except Exception as e:
+        print("rgb exception ",e)
+
+    return rgb_out
+
+class VALUE():
+    def __init__(self,v=0,_min=0,_max=255):
+        self._val = v
+        self._max = _max
+        self._min = _min
+
+    def _check(self):
+        if self._val > self._max:
+            self._val = self._max
+        if self._val < self._min:
+            self._val = self._min
+
+    def get(self):
+        self._check()
+        return self._val
+
+    def set(self,val):
+        if val <= self._max and val >= self._min:
+            self._val = val
+
+    def inc(self,v):
+        self._val += v
+        self._check()
+
+
+class ELEM_KILLGROUP():
+    def __init__(self):
+        self.data = []
+    def insert(self,elm):
+        if elm not in self.data:
+            self.data.append(elm)
+            return 1
+        return 0
+    def clean(self,elm):
+        v = elm.val
+        for i in self.data:
+            i.clear()
+        #elm.set(v)
+        return v
+
+class CALLBACK():
+    def __init__(self):
+        self._cb  = self.dummy
+        self.ok = 0
+    def cb(self,*args):
+        if self.ok:
+            print("CALLBACK.cb",args)
+            #try:
+            self._cb(args)
+            #except Exception as e:
+            #    print(" Exception CALLBACK.cb",args)
+    def dummy(self,arg):
+        print("CALLBACK.dummy",arg)
+    def set(self,cb):
+        self._cb = cb
+        self.ok = 1
+        print("CALLBACK",cb)
+
+
+class ELEM_BUF():
+    def __init__(self,kill=None,name="ELEM_BUF"):
+        self.val = VALUE() #0
+        self.increment = 10 
+        self.name = name
+        self.cb_on  = CALLBACK()
+        self.cb_off = CALLBACK()
+        self.nr_on  = [0]
+        self.nr_off = [0]
+        self.color = [0,255,0]
+        self.color_on = [255,255,0]
+        self.type="flash" #"toggle" #"flash",fade
+        self.killgroup = kill 
+        self.events = []
+    def _rep__(self):
+        x="<ELEM_BUF name:{} val:{} id:{}>".format(self.name,self.val.get(), id(self))
+    def get_event(self):
+        out = self.events[:]
+        self.events = []
+        return out
+
+    def get(self):
+        return self.val.get()
+
+    def get_color(self):
+        if self.val.get():
+            return self.color_on
+        return self.color
+
+    def clean(self):
+        self.val.set(0)
+
+    def press(self):
+        #print("ELEM_BUF.press",[self.name,self.type,self.val.get()])
+        if self.type == "fader":
+            self.inc(self.increment)
+
+        if self.type == "toggle":
+            if self.val.get():
+                self.val.set(0)
+            else:
+                self.val.set(1)
+
+        if self.type == "flash":
+            self.val.set(1)
+        
+        self.events.append("press")
+        self.cb_on.cb("ho")
+
+    def release(self):
+        if self.type == "fader":
+            self.inc(-self.increment)
+        if self.type == "flash":
+            self.val.set(0)
+        self.events.append("release")
+
+    def inc(self,v):
+        self.val.inc(v)
+
+class Layout():
+    def __init__(self,master):
+        self.master = master
+    def pack(self,**args):
+        pass
+    def grid(self,**args):
+        pass
+    def bind(self,**args):
+        pass
+
+def get_font_hight(font):
+    fr = font.render("test_font_hight" ,1, (0,0,0))
+    r = fr.get_rect()
+    h = r[3]
+    return h
+
+
+
+def draw_bd(pos=[0,0,10,10],delta=0):
+    d = delta
+    xpos = ( 
+            (pos[0]-d          ,pos[1]-d),
+            (pos[0]+pos[2]+d-1 ,pos[1]-d),
+            (pos[0]+pos[2]+d-1 ,pos[1]+pos[3]+d-1),
+            (pos[0]-d          ,pos[1]+pos[3]+d-1)
+            )
+    i_old = None
+    ypos = []
+    for i in xpos:
+        if i_old:
+            ypos.append( (i_old,i)   )
+        i_old = i
+
+    ypos.append( (i_old,xpos[0])   )
+    return ypos
+
+def check_area_v(v1,v2,event_v):#elm_pos,event_pos):
+    if event_v < v1+1: 
+        return 0
+    if event_v > v2-1: 
+        return 0
+    return 1
+
+def check_area(pos,event_pos):
+    v2 = pos[0]+pos[2]
+    x = check_area_v(pos[0],v2,event_pos[0])
+    v2 = pos[1]+pos[3]
+    y = check_area_v(pos[1],v2,event_pos[1])
+    if x and y:
+        return 1
+
+
+def check_area2_dir(R1,R2):
+    r2 = R2[:] #mouse_box
+    xd = 1
+    yd = 1
+    if r2[0] > r2[2]:
+        r2[0],r2[2] = r2[2],r2[0]
+        xd=-1
+    if r2[1] > r2[3]:
+        r2[1],r2[3] = r2[3],r2[1]
+        yd=-1
+    #print("check_area2_dir",xd,yd)
+    return xd,yd,r2
+
+
+def check_area2(R1,R2): #pos,mouse_box
+    xd,yd,r2 = check_area2_dir(R1,R2)
+
+    btn_box = R1[:] #btn_box
+    w=btn_box[2]
+    h=btn_box[3]
+
+    p1 = [btn_box[0],btn_box[1]]
+    p4 = [btn_box[0]+w,btn_box[1]+h]
+
+    x=0
+    if r2[2] > p1[0] and r2[0] < p4[0]:
+        x+=1
+    y=0
+    if r2[3] > p1[1] and r2[1] < p4[1]:
+        y+=1
+
+    if x and y:#> 4:
+        #print("btn",R1,"mouse",R2)
+        #print("btn",btn_box,"mouse",r2)
+        #print("area2",x,y)
+        return 1 
+
+
+class Button():
+    def __init__(self,window,pos):
+        self.window = window
+        self.event_pos = [0,0]
+        self.font0 = ("freesans-bold",16)
+        self.w = 20
+        self.h = 10
+        self.pos = pos
+        self.rel_pos = [0,0]
+        self.fader = "h" #v
+        self.ATTR = "XX"
+        self.ID = "0"
+
+        self.btn1 = ELEM_BUF() 
+        self.btn1.name = "BUTTON"
+        self.btn1.nr_on  = [1,3]
+        self.btn1.nr_off = [1,3]
+        #self.btn1.color = LIGHTGRAY 
+        self.btn1.color = GRAY 
+        self.btn1.color_on = RED 
+
+        self.btn2 = ELEM_BUF() # sel elem
+        self.btn2.name = "SELECT BUF"
+        self.btn2.nr_on  = [2]
+        self.btn2.nr_off = [0]
+        self.btn2.color = GRAY 
+        self.btn2.color_on = YELLOW
+        self.btn2.type = "toggle"
+
+        self.btn3 = ELEM_BUF() 
+        self.btn3.name = "MOUSE FOCUS"
+        self.btn3.color = GRAY 
+        self.btn3.color_on = WHITHE
+
+        self.btn4 = ELEM_BUF() 
+        self.btn4.name = "MOUSE ENCODER"
+        self.btn4.increment = 4.4
+        self.btn4.type = "fader"
+        self.btn4.nr_on  = [4]
+        self.btn4.nr_off = [5]
+        self.btn4.color = GRAY 
+        self.btn4.color_on = WHITHE
+
+        self.btns = []
+        self.btns.append(self.btn1)
+        self.btns.append(self.btn2)
+        self.btns.append(self.btn3)
+        self.btns.append(self.btn4)
+
+        self.__layout = Layout(self)
+        self.pack = self.__layout.pack
+        self.grid = self.__layout.grid
+        self.bind = self.__layout.bind
+        self.text = "line1\nline2"
+        self.type = "toggle" # flash, kill
+        self.dbg = 0
+        self.text2 = []
+
+    def __repr__(self):
+        x="<sdl.BUTTON name:{} ID:{}-{:8} btn1:{:03} val2:{:03} id at {}>"
+        x=x.format(self.btn1.name,self.ID,self.ATTR,self.btn1.val.get(),self.btn4.val.get(), id(self))
+        return x
+    def check(self):
+        if self.dbg:
+            self.text2 = []
+            #self.text2.append(self.val)
+            b = []
+            for btn in self.btns:
+                b.append(btn.get())
+            self.text2.append(b)
+            self.text2.append(self.btn1.type)
+
+        self._check_event()
+        self._check_min_hight()
+
+    def draw(self,text="GOBO1"):
+        self.check()
+
+        #self.window.set_alpha(128)  
+
+        self._draw_bg()
+        self._draw_fader()
+        self._draw_font(text="")
+            
+        rgb = self.btn2.get_color()
+
+        self._draw_bd(color=rgb) 
+        self._draw_bd(delta=-1)
+
+        rgb = self.btn3.get_color()
+        self._draw_bd(delta=-2,color=rgb)
+
+    def get_rect(self):
+        self.check()
+        return self.pos[:]
+
+
+    def _check_min_hight(self):
+        c = 1+ self.text.count("\n") #+1
+        c += len(self.text2)
+
+        fh = get_font_hight(self.font0)
+        h = (fh+1)*c +6#8 #+8
+        if self.pos[3] < h:
+            self.pos[3] = h #ah+20
+
+    def _draw_bg(self):
+        pos = self.pos
+
+        rgb = self.btn1.get_color()
+        rgb = check_rgb(rgb)
+        #pygame.draw.rect(self.window,rgb,pos)
+
+    def _draw_fader(self):
+        rgb = [0,200,0]
+        rgb = self.btn4.color_on
+        pos2 = self.pos[:]
+        hight = pos2[3] 
+        v = self.btn4.val.get() #self.val.get()
+        fh = get_font_hight(self.font0)
+        _max_val = self.btn4.val._max
+        if self.fader == "h":
+            pos2[1] += 2 #fh+2
+            pos2[3] = 4 #fh+2
+            if v > 0: 
+                pos2[2] = int(pos2[2]* v/_max_val)
+            else:
+                pos2[2] = 4
+            #pygame.draw.rect(self.window,rgb,pos2)
+        elif self.fader == "v":
+            if v > 0: 
+                pos2[1] += int((hight-20)* v/_max_val)
+                pos2[3] = 20
+            else:
+                pos2[3] = 20 
+            pos2[0] += 6
+            pos2[2] -= 12 
+            #pygame.draw.rect(self.window,rgb,pos2)
+
+    def _draw_font(self,text=""):
+        pos = self.pos
+
+        a = pos[0]+4
+        r = pos[1]+4
+        v = "{:4.02f}".format(self.btn4.val.get()) 
+
+        lines = self.text.split("\n")
+        lines.extend(self.text2)
+
+        for i in lines:
+            i = str(i)
+            if "<ival%>" in i:
+                v=float(v)
+                v=v/self.btn4.val._max*100
+                v=int(v)
+                i = i.replace("<ival%>",str(v))
+            if "<ival>" in i:
+                i = i.replace("<ival>",str(int(float(v))))
+            if "<val>" in i:
+                i = i.replace("<val>",v)
+            fr = self.font0.render(i ,1, (0,0,0))
+
+            fr_r=fr.get_rect()
+            p2 = [pos[0]+4,r,fr_r[2],fr_r[3]]
+            if 0:# dbg # bg highlight
+                #pygame.draw.rect(self.window,[0,0,255],p2)
+                pass
+
+            #self.window.blit(fr,(a,r))
+            r+=fr_r[3]+1
+
+    def _set_mouse_focus(self,state):
+        if state:
+            self.btn3.press() # mouse focus on
+        else:
+            self.btn3.release()
+
+    def _check_event(self):
+        pass
+    def _draw_bd(self,delta=0,color=GRAY):#BLACK):
+        l_pos = draw_bd(pos=self.pos,delta=delta)
+        for i in l_pos:
+            #pygame.draw.aaline(self.window,color,i[0],i[1],1)
+            pass
+
+
+    def event(self,event=None):
+        r_event = {}
+        if "pos" in event.dict:
+            self.event_pos = event.pos[:]
+
+            
+            update_rel_pos = 0
+            if "buttons" in event.dict:
+                if event.dict["buttons"][0]:
+                    update_rel_pos = 1
+
+            if "button" in event.dict:
+                if event.dict["button"] == 1:
+                    update_rel_pos = 1
+
+            if update_rel_pos:
+                    rel = [0,0]
+                    rel[0] = self.event_pos[0] -self.pos[0]-4
+                    rel[0] = rel[0]/(self.pos[2]-8)
+                    rel[1] = self.event_pos[1] -self.pos[1]-4
+                    rel[1] = rel[1]/(self.pos[3]-8)
+
+                    if rel[0] < 0:
+                        rel[0] = 0
+                    if rel[0] > 1:
+                        rel[0] = 1
+
+                    if rel[1] < 0:
+                        rel[1] = 0
+                    if rel[1] > 1:
+                        rel[1] = 1
+
+                    #print("RELPOS",rel)
+                    self.rel_pos = rel
+            self._check_event()
+
+        self._set_mouse_focus(0)
+        if check_area(self.pos,self.event_pos):
+            self._set_mouse_focus(1)
+
+
+            if "button" in event.dict:
+                mode = ""
+                if event.type == 5:
+                    mode = "press"
+                if event.type == 6: 
+                    mode = "release"
+
+                e = [event.button,mode]
+                #print("e",e)
+                for btn in self.btns: 
+                    if e[0] in btn.nr_on  and e[1] == "press":
+                        btn.press()
+                    if e[0] in btn.nr_off and e[1] == "release":
+                        btn.release()
+                    re = btn.get_event()
+                    if re and btn.name not in ['MOUSE FOCUS']:
+                        #print("----------------",btn.name,re)
+                        r_event[btn.name] = re
+        return r_event
+
+
+def draw_mouse_box(window,pos1,pos2,color=[128,128,128],text=1):
+    color = [200,0,0,127]
+    
+    if text:
+        fr = font15.render("A" ,1, (200,200,200))
+        #window.blit(fr,pos1)
+
+        fr = font15.render("B" ,1, (200,200,200))
+        #window.blit(fr,[pos2[0]-10,pos2[1]-10])
+
+    # h unten
+    _pos1 = [pos1[0],pos2[1]]
+    _pos2 = [pos2[0],pos2[1]]
+    #pygame.draw.aaline(window,color,_pos1,_pos2,1)
+
+    color = [255,255,0,127]
+    # h rechts
+    _pos1 = [pos2[0],pos1[1]]
+    _pos2 = [pos2[0],pos2[1]]
+    #pygame.draw.aaline(window,color,_pos1,_pos2,1)
+
+
+    color = [0,200,0,127]
+    # h links
+    _pos1 = [pos1[0],pos1[1]]
+    _pos2 = [pos1[0],pos2[1]]
+    #pygame.draw.aaline(window,color,_pos1,_pos2,1)
+
+
+    color = [0,0,200,127]
+    # h oben
+    _pos1 = [pos1[0],pos1[1]]
+    _pos2 = [pos2[0],pos1[1]]
+    #pygame.draw.aaline(window,color,_pos1,_pos2,1)
+