View Single Post
Old 02-18-2017, 09:53 PM   #3
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,960
Default

very quick test

Code:
  function Run()
    item = reaper.GetSelectedMediaItem(0,0)
    DEFINE_obj()
    GUI_draw() 
    MOUSE_get()
    gfx.update()
    local char = gfx.getchar() 
    if char == 32 then reaper.Main_OnCommandEx(40044, 0,0) end
    if char == 27 then gfx.quit() end     
    if char ~= -1 then reaper.defer(Run) else gfx.quit() end   
  end
  ---------------------------------------  
  function GUI_slider(obj_t, noval)
    if not obj_t then return end
    local x,y,w,h, val = obj_t.x,obj_t.y,obj_t.w,obj_t.h,obj_t.val
    gfx.set(1,1,1,1)
    gfx.rect(x,y,w,h,0)
    gfx.a = 0.2
    if not noval then gfx.rect(x,y,w*val,h,1) end
    gfx.a = 1
    local txt_offs = 5
    gfx.x, gfx.y = x+txt_offs,y+txt_offs
    txt = obj_t.name..':'..math.floor(val*100)/100
    gfx.drawstr(txt)
  end
  ---------------------------------------
  function GUI_draw()
    if item then 
      tk = reaper.GetActiveTake(item)
      _, tk_name = reaper.GetSetMediaItemTakeInfo_String( tk, 'P_NAME', '', 0 )
      gfx.x, gfx.y = 5,5 gfx.drawstr(tk_name)
      GUI_slider(obj.fader_vol)
      GUI_slider(obj.fader_f_in)
      GUI_slider(obj.fader_f_out)
      GUI_slider(obj.fader_point_st, true)
      GUI_slider(obj.fader_point_end, true)
    end
    
    gfx.update()
  end
  ------------------------------------------------------------------ 
  function MOUSE_match(b)
    if b and b.x then
      local mouse_y_match = b.y
      local mouse_h_match = b.y+b.h
      if mouse.mx > b.x 
          and mouse.mx < b.x+b.w 
          and mouse.my > mouse_y_match 
          and mouse.my < mouse_h_match 
          then return true 
      end 
    end
  end 
  function MOUSE_slider(obj_t, nolim)
    if mouse.LMB_state 
      and not mouse.last_LMB_state 
      and MOUSE_match(obj_t) then
          mouse.last_obj_val =obj_t.val
          mouse.last_obj = obj_t.name
        end
        if mouse.LMB_state and mouse.last_obj == obj_t.name then
          if not nolim then new_val = F_limit(mouse.last_obj_val + mouse.dx*0.005,0,1)
            else           new_val =         mouse.last_obj_val + mouse.dx*0.005,0,1
          end
          obj_t.func(new_val)
        end    
  end
  ---------------------------------------
  function MOUSE_get()
    mouse.abs_x, mouse.abs_y = reaper.GetMousePosition()
    mouse.mx = gfx.mouse_x
    mouse.my = gfx.mouse_y
    mouse.LMB_state = gfx.mouse_cap&1 == 1 
    
    -- dx/dy
      if not mouse.last_LMB_state and mouse.LMB_state then 
        mouse.LMB_stamp_x = mouse.mx
        mouse.LMB_stamp_y = mouse.my
      end    
      if mouse.LMB_state then 
        mouse.dx = mouse.mx - mouse.LMB_stamp_x
        mouse.dy = mouse.my - mouse.LMB_stamp_y
      end
    
    if item then 
      MOUSE_slider(obj.fader_vol)
      MOUSE_slider(obj.fader_f_in)
      MOUSE_slider(obj.fader_f_out)
      MOUSE_slider(obj.fader_point_st, true)
      MOUSE_slider(obj.fader_point_end, true)
    end
    
    -- reset mouse context/doundo
      if mouse.last_LMB_state and not mouse.LMB_state then 
        mouse.last_obj = nil
        mouse.last_obj_val = nil
        mouse.dx = 0
        mouse.dy = 0
      end
  
    -- mouse release
      mouse.last_LMB_state = mouse.LMB_state  
      mouse.last_mx = mouse.mx
      mouse.last_my = mouse.my
  end
  --------------------------------------------
  function F_limit(val,min,max)
      if val == nil then return end
      local val_out = val
      if min and val < min then val_out = min end
      if max and val > max then val_out = max end
      return val_out
    end
  --------------------------------------------
  function DEFINE_obj()
    obj = {}
    if item then 
      local offs = 10
      local name_offs = 20
      local h_sl = 30
      obj.fader_vol = {x = offs,
                       y = offs+name_offs,
                       w =  gfx.w - offs *2,
                       h = h_sl,
                       name = 'Gain',
                       val = reaper.GetMediaItemInfo_Value( item, 'D_VOL' ),
                       func = function(v) 
                                reaper.SetMediaItemInfo_Value( item, 'D_VOL', v ) 
                                reaper.UpdateItemInProject(item)
                              end 
                       }
      obj.fader_f_in = {x = offs,
                       y = offs*2+h_sl+name_offs,
                       w =  gfx.w/2 - offs *2,
                       h = h_sl,
                       name = 'FadeIn',
                       val = reaper.GetMediaItemInfo_Value( item, 'D_FADEINLEN' ),
                       func = function(v) 
                                reaper.SetMediaItemInfo_Value( item, 'D_FADEINLEN', v ) 
                                reaper.UpdateItemInProject(item)
                              end 
                       }  
      obj.fader_f_out = {x = offs*3 + obj.fader_f_in.w,
                       y = offs*2+h_sl+name_offs,
                       w =  obj.fader_f_in.w,
                       h = h_sl,
                       name = 'FadeOut',
                       val = reaper.GetMediaItemInfo_Value( item, 'D_FADEOUTLEN' ),
                       func = function(v) 
                                reaper.SetMediaItemInfo_Value( item, 'D_FADEOUTLEN', v ) 
                                reaper.UpdateItemInProject(item)
                              end 
                       }  
      obj.fader_point_st = {x = offs,
                       y = offs*3+h_sl*2+name_offs,
                       w =  obj.fader_f_in.w,
                       h = h_sl,
                       name = 'StartPoint',
                       val = reaper.GetMediaItemInfo_Value( item, 'D_POSITION' ),         
                       func = function(v) 
                                reaper.SetMediaItemInfo_Value( item, 'D_POSITION', v ) 
                                reaper.UpdateItemInProject(item)
                              end 
                       }       
      obj.fader_point_end = {x =  offs*3 + obj.fader_f_in.w,
                       y = offs*3+h_sl*2+name_offs,
                       w =  obj.fader_f_in.w,
                       h = h_sl,
                       name = 'EndPoint',
                       val = reaper.GetMediaItemInfo_Value( item, 'D_LENGTH' ),         
                       func = function(v) 
                                reaper.SetMediaItemInfo_Value( item, 'D_LENGTH', v ) 
                                reaper.UpdateItemInProject(item)
                              end 
                       }                                                                                    
    end
  end
  --------------------------------------------
  mouse = {}
  MOUSE_get()
  gfx.init('test',300, 300,0,mouse.abs_x, mouse.abs_y)
  Run()

Last edited by mpl; 02-18-2017 at 10:01 PM.
mpl is offline   Reply With Quote