--[[
* Simple GUI template for scripts
* Author: EUGEN27771
* Author URI: http://forum.cockos.com/member.php?u=50462
* Licence: GPL v3
* Version: 1.0
]]
--[[
* Set Grid Toolbar.lua
* Author: Kokarev Maxim
* Author URI: http://rmmedia.ru/members/10967/
* Licence: GPL v3
* Version: 1.0
]]
--------------------------- Get Grid State ---------------------------
-----------------------Reference: mpl_Get project grid.lua-------------
function GetProjectGrid()
local grid_time,beats,measures,cml,grid_div
grid_time = reaper.BR_GetNextGridDivision(0)
beats, measures, cml = reaper.TimeMap2_timeToBeats(0, grid_time)
if measures == 0 then
grid_div = math.ceil(cml/beats)
if grid_div % 3 == 0 then grid_string = "1/"..math.floor(grid_div/3*2).."T"
else
grid_string = "1/"..math.floor(grid_div)
end
else
grid_string = measures
end
return grid_string
end
grid_str = GetProjectGrid()
--------------------------------------------------------------------------------
--- 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.rect(x, y, w, h, false) -- frame2
end
function Element:draw_text()
local x,y,w,h = self.x,self.y,self.w,self.h
end
----------------------------------------------------------------------------------------------------
--- Create Element Child Classes(Button) -------------------------------------------
----------------------------------------------------------------------------------------------------
local Button = {}
local Frame = {}
local Text = {}
extended(Button, Element)
extended(Frame, Element)
extended(Text, 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)
if self:mouseDown() then gfx.set(0.3, 0.3, 0.3, 1) end -- text tint when pressing the button
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.5 end -- button tint when hover
-- in elm L_down -----
if self:mouseDown() then a=a+4.8 end -- button tint when pressing
-- 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
self:draw_text() -- text
-- Draw label --------------
gfx.set(0.8, 0.8, 0.8, 1) -- set label color (button text color)
gfx.setfont(1, fnt, fnt_sz) -- set label fnt
self:draw_lbl() -- draw lbl
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--- Frame Class Methods -----------------------------------------------------
--------------------------------------------------------------------------------
function Frame:draw()
self:update_xywh() -- Update xywh(if wind changed)
local r,g,b,a = self.r,self.g,self.b,self.a
-- if self:mouseIN() then a=a+0.9 end --frame tint when pressed
gfx.set(r,g,b,a) -- set frame color
self:draw_frame() -- draw frame
end
---------------------- Main Window and Text Output ---------------------
function Text:draw()
gfx.setfont(1, "HaxrCorp S12", 12)
gfx.set(0.8,0.7,0.3,1)
local str_w, str_h = gfx.measurestr(grid_string)
local x,y,w,h = self.x,self.y,self.w,self.h
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
gfx.x = x+(w - str_w) / 2
gfx.y = y+(h - str_h) / 2
gfx.drawstr(grid_string)
end
----------------------------------------------------------------------------------------------------
--- START --------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--- Buttons and Functions ------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
local btn1 = Button:new(73,20,50,30, 0.3,0.3,0.3,1, "1","HaxrCorp S12",15, 0 )
btn1.onClick =
function ()
reaper.Main_OnCommand(40781, 0) ---Grid: Set to 1
GetProjectGrid()
end
local btn2 = Button:new(126,20,50,30, 0.3,0.3,0.3,1, "1/2","HaxrCorp S12",15, 0 )
btn2.onClick =
function ()
reaper.Main_OnCommand(40780, 0) ---Grid: Set to 1/2
GetProjectGrid()
end
local btn3 = Button:new(179,20,50,30, 0.3,0.3,0.3,1, "1/4","HaxrCorp S12",15, 0 )
btn3.onClick =
function ()
reaper.Main_OnCommand(40779, 0) ---Grid: Set to 1/4
GetProjectGrid()
end
local btn4 = Button:new(232,20,50,30, 0.3,0.3,0.3,1, "1/8","HaxrCorp S12",15, 0 )
btn4.onClick =
function ()
reaper.Main_OnCommand(40778, 0) ---Grid: Set to 1/8
GetProjectGrid()
end
local btn5 = Button:new(285,20,50,30, 0.3,0.3,0.3,1, "1/16","HaxrCorp S12",15, 0 )
btn5.onClick =
function ()
reaper.Main_OnCommand(40776, 0) ---Grid: Set to 1/16
GetProjectGrid()
end
local btn6 = Button:new(338,20,50,30, 0.3,0.3,0.3,1, "1/32","HaxrCorp S12",15, 0 )
btn6.onClick =
function ()
reaper.Main_OnCommand(40775, 0) ---Grid: Set to 1/32
GetProjectGrid()
end
local btn7 = Button:new(391,20,50,30, 0.3,0.3,0.3,1, "1/64","HaxrCorp S12",15, 0 )
btn7.onClick =
function ()
reaper.Main_OnCommand(40774, 0) ---Grid: Set to 1/64
GetProjectGrid()
end
local btn8 = Button:new(444,20,50,30, 0.3,0.3,0.3,1, "1/128","HaxrCorp S12",15, 0 )
btn8.onClick =
function ()
reaper.Main_OnCommand(41047, 0) ---Grid: Set to 1/128
GetProjectGrid()
end
local btn9 = Button:new(498,20,50,30, 0.3,0.3,0.3,1, "-3-","HaxrCorp S12",15, 0 )
btn9.onClick =
function ()
reaper.Main_OnCommand(reaper.NamedCommandLookup('_SWS_AWTOGGLETRIPLET'), 0) --- Toggle triplets
GetProjectGrid()
end
local Button_TB = {btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9}
-----
local W_Frame = Frame:new(20,20,50,30, 0,0.5,0,0.4 )
local Frame_TB = {W_Frame}
-----
local W_Text = Text:new(20,20,50,30, 0,0.5,0,0.4 )
local Text_TB = {W_Text}
----------------------------------------------------------------------------------------------------
--- Main DRAW function -------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
function DRAW()
for key,btn in pairs(Button_TB) do btn:draw() end
for key,frame in pairs(Frame_TB) do frame:draw() end
for key,text in pairs(Text_TB) do text:draw() end
end
--------------------------------------------------------------------------------
-- INIT --------------------------------------------------------------------
--------------------------------------------------------------------------------
function Init()
-- Some gfx Wnd Default Values --
local R,G,B = 51,51,51 -- 0..255 form (background color)
local Wnd_bgd = R + G*256 + B*65536 -- red+green*256+blue*65536
local Wnd_Title = "Set Grid Toolbar" -- Main title
local Wnd_Dock,Wnd_X,Wnd_Y = 0,350,200
Wnd_W,Wnd_H = 567,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()