--[[
* Simple GUI template for scripts
* Author: EUGEN27771
* Author URI: http://forum.cockos.com/member.php?u=50462
* Licence: GPL v3
* Version: 1.0
]]
--------------------------------------------------------------------------------
--- Simple Element Class ---------------------------------------------------
--------------------------------------------------------------------------------
local Element = {}
function Element:new(x,y,w,h, r,g,b,a, lbl,fnt,fnt_sz, norm_val,norm_val2)
local elm = {}
elm.def_xywh = {x,y,w,h,fnt_sz} -- its default coord,used for Zoom etc
elm.x, elm.y, elm.w, elm.h = x, y, w, h
elm.r, elm.g, elm.b, elm.a = r, g, b, a
elm.lbl, elm.fnt, elm.fnt_sz = lbl, fnt, fnt_sz
elm.norm_val = norm_val
elm.norm_val2 = norm_val2
------
setmetatable(elm, self)
self.__index = self
return elm
end
--------------------------------------------------------------
--- Function for Child Classes(args = Child,Parent Class) ----
--------------------------------------------------------------
function extended(Child, Parent)
setmetatable(Child,{__index = Parent})
end
--------------------------------------------------------------
--- Element Class Methods(Main Methods) ------------------
--------------------------------------------------------------
function Element:update_xywh()
if not Z_w or not Z_h then return end -- return if zoom not defined
self.x, self.w = math.ceil(self.def_xywh[1]* Z_w) , math.ceil(self.def_xywh[3]* Z_w) -- upd x,w
self.y, self.h = math.ceil(self.def_xywh[2]* Z_h) , math.ceil(self.def_xywh[4]* Z_h) -- upd y,h
if self.fnt_sz then --fix it!--
self.fnt_sz = math.max(9,self.def_xywh[5]* (Z_w+Z_h)/2)
self.fnt_sz = math.min(22,self.fnt_sz)
end
end
------------------------
function Element:pointIN(p_x, p_y)
return p_x >= self.x and p_x <= self.x + self.w and p_y >= self.y and p_y <= self.y + self.h
end
--------
function Element:mouseIN()
return gfx.mouse_cap&1==0 and self:pointIN(gfx.mouse_x,gfx.mouse_y)
end
------------------------
function Element:mouseDown()
return gfx.mouse_cap&1==1 and self:pointIN(mouse_ox,mouse_oy)
end
--------
function Element:mouseClick()
return gfx.mouse_cap&1==0 and last_mouse_cap&1==1 and
self:pointIN(gfx.mouse_x,gfx.mouse_y) and self:pointIN(mouse_ox,mouse_oy)
end
------------------------
function Element:mouseR_Down()
return gfx.mouse_cap&2==2 and self:pointIN(mouse_ox,mouse_oy)
end
--------
function Element:mouseM_Down()
return gfx.mouse_cap&64==64 and self:pointIN(mouse_ox,mouse_oy)
end
------------------------
function Element:draw_frame()
local x,y,w,h = self.x,self.y,self.w,self.h
gfx.rect(x, y, w, h, false) -- frame1
gfx.roundrect(x, y, w-1, h-1, 3, true) -- frame2
end
----------------------------------------------------------------------------------------------------
--- Create Element Child Classes(Button,Slider,Knob) -------------------------------------------
----------------------------------------------------------------------------------------------------
local Button = {}
extended(Button, Element)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--- Button Class Methods ---------------------------------------------------
--------------------------------------------------------------------------------
function Button:draw_body()
gfx.rect(self.x,self.y,self.w,self.h, true) -- draw btn body
end
--------
function Button:draw_lbl()
local x,y,w,h = self.x,self.y,self.w,self.h
local lbl_w, lbl_h = gfx.measurestr(self.lbl)
gfx.x = x+(w-lbl_w)/2; gfx.y = y+(h-lbl_h)/2
gfx.drawstr(self.lbl)
end
------------------------
function Button:draw()
self:update_xywh() -- Update xywh(if wind changed)
local r,g,b,a = self.r,self.g,self.b,self.a
local fnt,fnt_sz = self.fnt, self.fnt_sz
-- Get mouse state ---------
-- in element --------
if self:mouseIN() then a=a+0.1 end
-- in elm L_down -----
if self:mouseDown() then a=a+0.2 end
-- in elm L_up(released and was previously pressed) --
if self:mouseClick() and self.onClick then self.onClick() end
-- Draw btn body, frame ----
gfx.set(r,g,b,a) -- set body color
self:draw_body() -- body
self:draw_frame() -- frame
-- Draw label --------------
gfx.set(0.8, 0.8, 0.8, 1) -- set label color
gfx.setfont(1, fnt, fnt_sz) -- set label fnt
self:draw_lbl() -- draw lbl
end
----------------------------------------------------------------------------------------------------
--- START --------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--- Buttons and Functions ------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
local btn1 = Button:new(20,20,50,30, 0.3,0.3,0.3,1, "1","Arial",15, 0 )
btn1.onClick =
function ()
reaper.Main_OnCommand(40781, 0) ---Track: Select all tracks
end
local btn2 = Button:new(73,20,50,30, 0.3,0.3,0.3,1, "1/2","Arial",15, 0 )
btn2.onClick =
function ()
reaper.Main_OnCommand(40780, 0) ---Track: Select all tracks
end
local btn3 = Button:new(126,20,50,30, 0.3,0.3,0.3,1, "1/4","Arial",15, 0 )
btn3.onClick =
function ()
reaper.Main_OnCommand(40779, 0) ---Track: Select all tracks
end
local btn4 = Button:new(179,20,50,30, 0.3,0.3,0.3,1, "1/8","Arial",15, 0 )
btn4.onClick =
function ()
reaper.Main_OnCommand(40778, 0) ---Track: Select all tracks
end
local btn5 = Button:new(232,20,50,30, 0.3,0.3,0.3,1, "1/16","Arial",15, 0 )
btn5.onClick =
function ()
reaper.Main_OnCommand(40776, 0) ---Track: Select all tracks
end
local btn6 = Button:new(285,20,50,30, 0.3,0.3,0.3,1, "1/32","Arial",15, 0 )
btn6.onClick =
function ()
reaper.Main_OnCommand(40775, 0) ---Track: Select all tracks
end
local btn7 = Button:new(338,20,50,30, 0.3,0.3,0.3,1, "1/64","Arial",15, 0 )
btn7.onClick =
function ()
reaper.Main_OnCommand(40774, 0) ---Track: Select all tracks
end
local btn8 = Button:new(391,20,50,30, 0.3,0.3,0.3,1, "1/128","Arial",15, 0 )
btn8.onClick =
function ()
reaper.Main_OnCommand(41047, 0) ---Track: Select all tracks
end
local btn9 = Button:new(444,20,50,30, 0.3,0.3,0.3,1, "-3-","Arial",15, 0 )
btn9.onClick =
function ()
reaper.Main_OnCommand(reaper.NamedCommandLookup('_SWS_AWTOGGLETRIPLET'), 0)
end
local Button_TB = {btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9}
-----
-----
----------------------------------------------------------------------------------------------------
--- Main DRAW function -------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
function DRAW()
for key,btn in pairs(Button_TB) do btn:draw() end
end
--------------------------------------------------------------------------------
-- INIT --------------------------------------------------------------------
--------------------------------------------------------------------------------
function Init()
-- Some gfx Wnd Default Values --
local R,G,B = 51,51,51 -- 0..255 form
local Wnd_bgd = R + G*256 + B*65536 -- red+green*256+blue*65536
local Wnd_Title = "Set Grid Toolbar"
local Wnd_Dock,Wnd_X,Wnd_Y = 0,100,320
Wnd_W,Wnd_H = 512,70 -- global values(used for define zoom level)
-- Init window ------
gfx.clear = Wnd_bgd
gfx.init( Wnd_Title, Wnd_W,Wnd_H, Wnd_Dock, Wnd_X,Wnd_Y )
-- Init mouse last --
last_mouse_cap = 0
last_x, last_y = 0, 0
mouse_ox, mouse_oy = -1, -1
end
----------------------------------------
-- Mainloop ------------------------
----------------------------------------
function mainloop()
-- zoom level --
Z_w, Z_h = gfx.w/Wnd_W, gfx.h/Wnd_H
if Z_w<0.6 then Z_w = 0.6 elseif Z_w>2 then Z_w = 2 end
if Z_h<0.6 then Z_h = 0.6 elseif Z_h>2 then Z_h = 2 end
-- mouse and modkeys --
if gfx.mouse_cap&1==1 and last_mouse_cap&1==0 or -- L mouse
gfx.mouse_cap&2==2 and last_mouse_cap&2==0 or -- R mouse
gfx.mouse_cap&64==64 and last_mouse_cap&64==0 then -- M mouse
mouse_ox, mouse_oy = gfx.mouse_x, gfx.mouse_y
end
Ctrl = gfx.mouse_cap&4==4
Shift = gfx.mouse_cap&8==8
Alt = gfx.mouse_cap&16==16 -- Shift state
-------------------------
-- DRAW,MAIN functions --
DRAW() -- Main()
-------------------------
-------------------------
last_mouse_cap = gfx.mouse_cap
last_x, last_y = gfx.mouse_x, gfx.mouse_y
gfx.mouse_wheel = 0 -- reset gfx.mouse_wheel
char = gfx.getchar()
if char==32 then reaper.Main_OnCommand(40044, 0) end -- play
if char==26 then reaper.Main_OnCommand(40029, 0) end ---undo
if char == 27 then gfx.quit() end -- escape
if char~=-1 then reaper.defer(mainloop) end -- defer
-----------
gfx.update()
-----------
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------
Init()
mainloop()