View Single Post
Old 11-30-2019, 03:42 PM   #623
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

- You can't use the Menubar code as-is because it's specifically written for the Menubar class - it references other properties and methods that the Button class won't have, and does things you don't need a Button to do.

- It's throwing an error because you can't pass a statement (col_fill = {.2,.8,.2}) as a function argument.

- You can't have more than one func = ___ - Lua will just overwrite the previous ones.

- func is also specifically for handling Button clicks; you have to specifically override the button's other methods.

This should do the trick. I've added a table, ToolbarButton, with the replacement button methods, and then assigned them to each button as it gets created.

Code:
-- NoIndex: true

-- Makes an 8 x 8 grid of buttons user can put actions into.
-- Grid can be modified by editing User Options section to get the desired grid size.
-- Script was made to solve problem of Reaper floating toolbar rearranging buttons if window was resized by accident or otherwise.

-- The Core library must be loaded prior to anything else

local lib_path = reaper.GetExtState("Lokasenna_GUI", "lib_path_v2")
if not lib_path or lib_path == "" then
    reaper.MB("Couldn't load the Lokasenna_GUI library. Please run 'Script: Set Lokasenna_GUI v2 library path.lua' in your Action List.", "Whoops!", 0)
    return
end
loadfile(lib_path .. "Core.lua")()

GUI.req("Classes/Class - Button.lua")()

-- If any of the requested libraries weren't found, abort the script.
if missing_lib then return 0 end


------------------------------------
-------- USER OPTIONS --------------
------------------------------------

local button_width      = 58    -- Set your button width  here
local button_height     = 28    -- Set your button height here
local buttons_per_row   = 8     -- Set how many buttons you want in each row here
local button_h_spacer   = 3     -- Set how much horizontal space between buttons here
local button_v_spacer   = 2     -- Set how much vertical   space between buttons here


------------------------------------
-------- Functions  ----------------
------------------------------------


local function btn_click(command)

	-- Be nice, give the user an Undo point
	reaper.Undo_BeginBlock()

    reaper.Main_OnCommand(reaper.NamedCommandLookup(command), 0)

	reaper.Undo_EndBlock("Toolbar Grid Template", 0)

    -- Exit the script on the next update
	GUI.quit = false

end


------------------------------------
-------- GUI Elements --------------
------------------------------------

local elms = {}

-- Define your buttons below.

-- Copy, delete, or comment out label/command lines below to get the number of buttons you want.

-- Label field is for text that will be displayed on the the button.
-- Example: label = "PLAY\n130%",
-- Note that the \n causes a line break and will display the text on two lines.

-- Command field is for the command ID from the actions list.
-- Example 1: command = 65535
-- Example 2: command = "_eed3041ab48a593fcd3909218c058b46"
-- Note that example 1 has no quotes as it is a native Reaper action.
-- Note that example 2 has quotes as it is a custom action.
-- Note that command = 65535 can be used as a spacer. It is a "no-op" action.
-- Text can still be displayed even if the button is a "no-op" action.

local toolbar_buttons = {
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
  { label = "",                   command = 65535 },
}


------------------------------------
-------- Window settings -----------
------------------------------------

for k in ipairs(toolbar_buttons) do

    total_buttons = k

end

local button_rows = (total_buttons / buttons_per_row)
local gui_w       = (button_width * buttons_per_row) + ((buttons_per_row - 1) * button_h_spacer)
local gui_h       = (button_height * button_rows) + ((button_rows - 1) * button_v_spacer)

GUI.name = "TOOLBAR GRID TEMPLATE"
--GUI.x, GUI.y, GUI.w, GUI.h = 0, 0, 972, 240
GUI.x, GUI.y, GUI.w, GUI.h = 0, 0, gui_w, gui_h
--GUI.x, GUI.y, GUI.w, GUI.h = 0, 0, gui_w, 208
GUI.anchor, GUI.corner = "mouse", "C"


------------------------------------
-------- GUI Elements cont. --------
------------------------------------


local ToolbarButton = {}

function ToolbarButton:onupdate()
  if self.hovered and not GUI.IsInside(self, GUI.mouse.x, GUI.mouse.y) then
    self.hovered = false
    self:redraw()

    -- Skip the rest of the update loop for this elm
    return true
  end
end

function ToolbarButton:onmouseover()
  if not self.hovered then
    self.hovered = true
    self:redraw()
  end
end

function ToolbarButton:draw()
  GUI.Button.draw(self)

  if self.hovered then
    GUI.color({1, 1, 1, 0.1})
    gfx.rect(self.x + 1 + 2 * self.state, self.y + 1 + 2 * self.state, self.w - 2, self.h - 2)
    gfx.a = 1
  end
end


local buttons_count     = -1    -- Leave this one alone or screwy things will happen

for k, v in ipairs(toolbar_buttons) do
  if row_buttons   == (buttons_per_row - 1) then    -- Set conditions for switching to a new row
      row_buttons   = 0
  else
      buttons_count = (buttons_count + 1)
  end

  elms["button_"..k] = {
    type = "Button",
    col_fill = {.2,.2,.2},
    z = (#toolbar_buttons - k) + 1,
    x = ((k - 1) % buttons_per_row) * (button_width + button_h_spacer),
    y = math.floor((k - 1) / buttons_per_row) *  (button_height + button_v_spacer),
    w = button_width,
    h = button_height,
    caption = v.label,
    func = btn_click,
    params = {v.command},       -- Send command parameter to the btn_click function so that it will accept a variable.
    tooltip = "I'm a button",
    onupdate = ToolbarButton.onupdate,
    onmouseover = ToolbarButton.onmouseover,
    draw = ToolbarButton.draw,
  }

end

GUI.CreateElms(elms)


GUI.Init()
GUI.Main()
This is one of the many things that will be easier to do in v3 - each event can have a before___ or after___ function attached to it rather than having to override the event method itself.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote