Browse Source

add: mouse-l,m,r, encoder=val, mouse_garb_box, esc=clear

micha 1 year ago
parent
commit
9b2033e8c8
2 changed files with 212 additions and 113 deletions
  1. 67 22
      LibreLightStart.py
  2. 145 91
      tool/sdl_elm.py

+ 67 - 22
LibreLightStart.py

@@ -3,6 +3,7 @@
 import time
 import time
 import random
 import random
 import os
 import os
+import sys
 import tool.movewin as movewin
 import tool.movewin as movewin
 
 
 # ===== GUI =========
 # ===== GUI =========
@@ -54,57 +55,56 @@ font22 = pygame.font.SysFont("FreeSans",22)
 fr = font.render("hallo" ,1, (200,0,255))
 fr = font.render("hallo" ,1, (200,0,255))
 
 
 start = time.time()
 start = time.time()
-
-# init
 table = []
 table = []
+
+r = 80
 i = 1
 i = 1
-r = 150
+
 bx = sdl_elm.Button(window,pos=[20,r,60,20])
 bx = sdl_elm.Button(window,pos=[20,r,60,20])
-bx.text = "FIX:{}".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
 bx.bg_on = [255,0,255]
 bx.bg_on = [255,0,255]
-bx.btn1.color_on = [255,0,55]
+bx.btn1.color_on = [255,0,155]
 bx.btn1.type = "flash"
 bx.btn1.type = "flash"
-#self.btn1.type = "flush"
 table.append(bx)
 table.append(bx)
-i += 1
-#r+=20
 r+=bx.get_rect()[3]
 r+=bx.get_rect()[3]
+
+i += 1
 bx = sdl_elm.Button(window,pos=[20,r,80,40])
 bx = sdl_elm.Button(window,pos=[20,r,80,40])
-bx.text = "FIX:{}".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
-#bx.bg_on = [255,0,255]
 table.append(bx)
 table.append(bx)
+r+=bx.get_rect()[3]
+
 i += 1
 i += 1
-#r+=60
 r+=bx.get_rect()[3]
 r+=bx.get_rect()[3]
 bx = sdl_elm.Button(window,pos=[20,r,80,40])
 bx = sdl_elm.Button(window,pos=[20,r,80,40])
-bx.text = "FIX:{}".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
-#bx.bg_on = [255,0,255]
 bx.font0 = pygame.font.SysFont("freesans",20)
 bx.font0 = pygame.font.SysFont("freesans",20)
+bx.val.set( 100)
+bx.fader = 0
 table.append(bx)
 table.append(bx)
-i += 1
-#r+=60
 r+=bx.get_rect()[3]
 r+=bx.get_rect()[3]
+
+i += 1
 bx = sdl_elm.Button(window,pos=[30,r,190,60])
 bx = sdl_elm.Button(window,pos=[30,r,190,60])
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
-#bx.bg_on = [255,0,255]
 bx.font0 = pygame.font.SysFont("freesans",20)
 bx.font0 = pygame.font.SysFont("freesans",20)
 bx.btn1.type = "flash"
 bx.btn1.type = "flash"
 table.append(bx)
 table.append(bx)
-i += 1
 r+=bx.get_rect()[3]
 r+=bx.get_rect()[3]
+
+i += 1
 bx = sdl_elm.Button(window,pos=[20,r,60,20])
 bx = sdl_elm.Button(window,pos=[20,r,60,20])
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
 bx.text = "FIX:{}\n<val>\nx".format(i+1)
-#bx.bg_on = [255,0,255]
 bx.font0 = pygame.font.SysFont("freesans",12)
 bx.font0 = pygame.font.SysFont("freesans",12)
 table.append(bx)
 table.append(bx)
 
 
-while 1:
+mouse_down = 0
+mouse_pos1 = [0,0]
+mouse_pos2 = [0,0]
+mouse_grab = []
 
 
+while 1:
     pygame.display.flip()
     pygame.display.flip()
-    #event()
     pos = [160,10,70,60]
     pos = [160,10,70,60]
     rgb = (0xdd,0xdd,0xdd,0)
     rgb = (0xdd,0xdd,0xdd,0)
     rgb = (0xaa,0xaa,0xaa,0)
     rgb = (0xaa,0xaa,0xaa,0)
@@ -128,8 +128,11 @@ while 1:
     rgb = (0x00,0x00,0x00,0)
     rgb = (0x00,0x00,0x00,0)
     fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
     fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
     window.blit(fr,pos[:2])
     window.blit(fr,pos[:2])
-
-
+    
+    pos = [160,200,80,20]
+    #fd = sdl_elm.Fader(window,pos)
+    #fd.draw()
+    
     pos = [160,90,70+80,20]
     pos = [160,90,70+80,20]
     pygame.draw.rect(window,rgb,pos)
     pygame.draw.rect(window,rgb,pos)
     b= int(t*10)
     b= int(t*10)
@@ -151,6 +154,12 @@ while 1:
 
 
     resize_changed = 0
     resize_changed = 0
     for event in pygame.event.get(): 
     for event in pygame.event.get(): 
+         
+        if "scancode" in event.dict:
+            if event.scancode == 9:
+                for t in table:
+                    t.btn2.clean()
+
         print("event",event)
         print("event",event)
         if event.type == pygame.QUIT:
         if event.type == pygame.QUIT:
             pygame.quit()
             pygame.quit()
@@ -164,5 +173,41 @@ while 1:
         for t in table:
         for t in table:
             t.event(event)
             t.event(event)
 
 
+        if "pos" in event.dict:
+            if "button" in event.dict:
+                if event.type == 5:#press
+                    mouse_down = 1
+                    mouse_pos1 = [event.pos[0],event.pos[1]]
+                if event.type == 6:#release
+                    mouse_down = 0
+
+                for btn in mouse_grab:
+                    btn.btn2.val = 1
+                mouse_grab = []
+            mouse_pos2 = [event.pos[0],event.pos[1]]
+
+    
+    if mouse_down:
+        d1 = mouse_pos1[0]-mouse_pos2[0]
+        d2 = mouse_pos1[1]-mouse_pos2[1] 
+        pix = 23
+        #print(d1,d2)
+        if ( d1 > pix or d1 < -pix)  or  ( d2 >pix or d2 < -pix):
+
+            sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
+            for t in table:
+                pos = t.get_rect()
+
+                mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
+
+                if sdl_elm.check_area2(pos,mpos):
+                    t._set_mouse_focus(1)
+                    mouse_grab.append(t)
+                else:
+                    t._set_mouse_focus(0)
+
     if resize_changed:# = True
     if resize_changed:# = True
         screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
         screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
+
+
+

+ 145 - 91
tool/sdl_elm.py

@@ -20,7 +20,7 @@ class VALUE():
         self._max = _max
         self._max = _max
         self._min = _min
         self._min = _min
     def set(self,val):
     def set(self,val):
-        if val <= self.max and val >= self.min:
+        if val <= self._max and val >= self._min:
             self._val = val
             self._val = val
     def _check(self):
     def _check(self):
         if self._val > self._max:
         if self._val > self._max:
@@ -57,7 +57,7 @@ class ELEM_BUF():
         self.val = 0
         self.val = 0
         self.color = [0,255,0]
         self.color = [0,255,0]
         self.color_on = [255,255,0]
         self.color_on = [255,255,0]
-        self.type="toggle" #"flash"
+        self.type="flash" #"toggle" #"flash"
         self.killgroup = kill 
         self.killgroup = kill 
 
 
     def get(self):
     def get(self):
@@ -95,6 +95,74 @@ class Layout():
     def bind(self,**args):
     def bind(self,**args):
         pass
         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(R1,R2): #pos,mouse_box
+    btn_box = R1[:] #btn_box
+    r2 = R2[:] #mouse_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]
+
+    if r2[0] > r2[2]:
+        r2[0],r2[2] = r2[2],r2[0]
+    if r2[1] > r2[3]:
+        r2[1],r2[3] = r2[3],r2[1]
+
+    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():
 class Button():
     def __init__(self,window,pos):
     def __init__(self,window,pos):
         self.window = window
         self.window = window
@@ -103,32 +171,37 @@ class Button():
         self.w = 20
         self.w = 20
         self.h = 10
         self.h = 10
         self.pos = pos
         self.pos = pos
+        self.fader = 1
 
 
-        self.btn1 = ELEM_BUF() #btn
-        #self.btn1.type = "flash"
+        self.btn1 = ELEM_BUF() # btn (background)
         self.btn1.color = [140,140,140]
         self.btn1.color = [140,140,140]
         self.btn1.color_on = [255,0,0]
         self.btn1.color_on = [255,0,0]
-        self.btn2 = ELEM_BUF() #sel
+
+        self.btn2 = ELEM_BUF() # sel
         self.btn2.color = [120,120,120]
         self.btn2.color = [120,120,120]
+        self.btn2.type ="toggle"
+
+        self.btn3 = ELEM_BUF() # mouse focus
+        self.btn3.color = [100,100,100]
+        self.btn3.color_on = [200,200,200]
 
 
         self.val = VALUE(0,0,256)
         self.val = VALUE(0,0,256)
-        self.val_inc = 10
+        self.val_inc = 4.4 #10
 
 
         self.__layout = Layout(self)
         self.__layout = Layout(self)
         self.pack = self.__layout.pack
         self.pack = self.__layout.pack
         self.grid = self.__layout.grid
         self.grid = self.__layout.grid
         self.bind = self.__layout.bind
         self.bind = self.__layout.bind
         self.text = "line1\nline2"
         self.text = "line1\nline2"
-        self.motion = 0
         self.type = "toggle" # flash, kill
         self.type = "toggle" # flash, kill
 
 
         self.text2 = []
         self.text2 = []
 
 
     def check(self):
     def check(self):
-        if 10:#dbg:
+        if 0:#dbg:
             self.text2 = []
             self.text2 = []
             #self.text2.append(self.val)
             #self.text2.append(self.val)
-            self.text2.append([self.btn1.get(),self.btn2.get()])
+            self.text2.append([self.btn1.get(),self.btn2.get(),self.btn3.get()])
             self.text2.append(self.btn1.type)
             self.text2.append(self.btn1.type)
 
 
         self._check_event()
         self._check_event()
@@ -140,43 +213,51 @@ class Button():
         self.window.set_alpha(128)  
         self.window.set_alpha(128)  
 
 
         self._draw_bg()
         self._draw_bg()
+        self._draw_fader()
         self._draw_font(text="")
         self._draw_font(text="")
             
             
         rgb = self.btn2.get_color()
         rgb = self.btn2.get_color()
 
 
         self._draw_bd(color=rgb) 
         self._draw_bd(color=rgb) 
         self._draw_bd(delta=-1)
         self._draw_bd(delta=-1)
-        self._draw_bd(delta=-2,highlight=1,color=rgb)
+
+        rgb = self.btn3.get_color()
+        self._draw_bd(delta=-2,color=rgb)
 
 
     def get_rect(self):
     def get_rect(self):
         self.check()
         self.check()
         return self.pos[:]
         return self.pos[:]
 
 
+
     def _check_min_hight(self):
     def _check_min_hight(self):
         c = 1+ self.text.count("\n") #+1
         c = 1+ self.text.count("\n") #+1
         c += len(self.text2)
         c += len(self.text2)
 
 
-        fr = self.font0.render("test_font_hight" ,1, (0,0,0))
-        #self.window.blit(fr,(pos[0]+4,r))
-        fr_r =fr.get_rect()
-        h = (fr_r[3]+1)*c +6#8 #+8
-        #print("-.",c,h,fr_r,self.pos,self.text)
-        #input()
+        fh = get_font_hight(self.font0)
+        h = (fh+1)*c +6#8 #+8
         if self.pos[3] < h:
         if self.pos[3] < h:
             self.pos[3] = h #ah+20
             self.pos[3] = h #ah+20
-            #self.bg = (0xff,0xaa,0xaa,127)
-
-    def _get_surface_center(self):
-        #text_rect = fr.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
-        pass
 
 
     def _draw_bg(self):
     def _draw_bg(self):
         pos = self.pos
         pos = self.pos
-        #rgb = self.bg
 
 
         rgb = self.btn1.get_color()
         rgb = self.btn1.get_color()
         pygame.draw.rect(self.window,rgb,pos)
         pygame.draw.rect(self.window,rgb,pos)
 
 
+    def _draw_fader(self):
+        rgb = [0,200,0]
+        pos2 = self.pos[:]
+        v = self.val.get()
+        fh = get_font_hight(self.font0)
+        if self.fader:
+            pos2[1] += 2 #fh+2
+            pos2[3] = 4 #fh+2
+            if v > 0: 
+                pos2[2] = int(pos2[2]* v/255)
+            else:
+                pos2[2] = 4
+            pygame.draw.rect(self.window,rgb,pos2)
+
     def _draw_font(self,text=""):
     def _draw_font(self,text=""):
         pos = self.pos
         pos = self.pos
 
 
@@ -201,60 +282,31 @@ class Button():
             self.window.blit(fr,(a,r))
             self.window.blit(fr,(a,r))
             r+=fr_r[3]+1
             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):
     def _check_event(self):
-        self.bd = [125,125,125]
-        self.motion = 0
-        if self.event_pos[0] < self.pos[0]+1: 
-            return #continue
-        if self.event_pos[0] > self.pos[0]+self.pos[2]-1: 
-            return #continue
-        
-        if self.event_pos[1] < self.pos[1]+1: 
-            return #continue
-        if self.event_pos[1] > self.pos[1]+self.pos[3]-1: 
-            return #continue
-
-        #print(">>",self.event_pos)
-        self.bd = [200,200,200]
-        self.motion = 1
-
-    def _draw_bd(self,delta=0,highlight=0,color=[0,0,0]):
-        pos = self.pos
-        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])   )
-
-        for i in ypos:
-            #print("ypos",i)
-            #pygame.draw.aaline(self.window,color,i[0],i[1],1)
+        pass
+    def _draw_bd(self,delta=0,color=[0,0,0]):
+        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)
             pygame.draw.aaline(self.window,color,i[0],i[1],1)
 
 
+
     def event(self,event=None):
     def event(self,event=None):
-        #print(self.pos)
         if "pos" in event.dict:
         if "pos" in event.dict:
             self.event_pos = event.pos
             self.event_pos = event.pos
             self._check_event()
             self._check_event()
 
 
-        if self.motion:
-            #print(self,"Motion.event",event)
+        self._set_mouse_focus(0)
+        if check_area(self.pos,self.event_pos):
+            self._set_mouse_focus(1)
+
 
 
             if "button" in event.dict:
             if "button" in event.dict:
-                #print()
-                #print("-","Button.event",event.dict,event.type,event.button,self.type)
-                #print(type(event))
                 mode = ""
                 mode = ""
                 if event.type == 5:
                 if event.type == 5:
                     mode = "press"
                     mode = "press"
@@ -279,39 +331,41 @@ class Button():
                 if e[0] in [5]: #mouse encoder 
                 if e[0] in [5]: #mouse encoder 
                     self.val.inc(-self.val_inc)
                     self.val.inc(-self.val_inc)
 
 
-def draw_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])
+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)
+    # 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 = [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,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)
+    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)