FL Tool Wheel

@xzenile / fl-tool-wheelv1.1.0Public

In FL Studio's Piano roll and Playlist, hold Mouse4 and scroll to step through the editing tools, or press a hotkey for a tool menu.

Files

Version 1.1.0 (current)

daw-fl-tool-wheel.luau11.9 KB

--[[
  rebind: min_sdk=3.11.1
  rebind: name=FL Tool Wheel
  rebind: version=1.1.0
  rebind: author=PLACEHOLDER
  rebind: description=In FL Studio's Piano roll and Playlist, hold Mouse4 and scroll to step through the editing tools, or press a hotkey for a tool menu.
  rebind: process=FL64
  rebind: process=FL.exe
  rebind: process=FL Studio
  rebind: permissions=none
  rebind: instance=single
]]

-- FL Tool Wheel
-- Each step sends FL's own one-letter tool key. The Piano roll and the Playlist
-- use the same letters for these tools. From Image-Line's shortcut page
-- (Piano roll action, Playlist action):
-- https://www.image-line.com/fl-studio-learning/fl-studio-online-manual/html/basics_shortcuts.htm
--   P Draw tool (pencil)  B Paint tool  D Delete tool  T Mute tool
--   C Slice tool          E Select tool Z Zoom tool    Y Playback tool
-- Image-Line notes that single-key shortcuts only work while
-- "Typing keyboard to piano" (Ctrl/Cmd+T) is off.

local KEY_TIP = "Takes effect after you restart the script."

local cfg = rb.ui.schema({
  hold_button = rb.ui.select("Mouse4", { "Mouse4", "Mouse5" }, {
    label = "Hold button (not the same as FL Window Wheel's)", group = "Wheel",
    tooltip = KEY_TIP,
  }),
  invert = rb.ui.toggle(false, { label = "Invert wheel direction", group = "Wheel" }),
  step_ms = rb.ui.slider(120, {
    label = "Minimum time between tools", group = "Wheel", min = 0, max = 600, step = 10, suffix = " ms",
  }),
  menu_key = rb.ui.keybind("LCtrl+LShift+T", {
    label = "Open tool menu", group = "Menu",
    tooltip = "Shows every tool; pick with 1 to 8 or Enter. You can also change it from Edit hotkeys.",
  }),
  board = rb.ui.keybind("LCtrl+LAlt+W", {
    label = "Edit hotkeys", group = "Menu",
    tooltip = "Opens Bind Board: pick a hotkey, press the new key, and it saves and works right away.",
  }),
  use_draw = rb.ui.toggle(true, { label = "Draw (P)", group = "Tools in the wheel" }),
  use_paint = rb.ui.toggle(true, { label = "Paint (B)", group = "Tools in the wheel" }),
  use_delete = rb.ui.toggle(true, { label = "Delete (D)", group = "Tools in the wheel" }),
  use_mute = rb.ui.toggle(true, { label = "Mute (T)", group = "Tools in the wheel" }),
  use_slice = rb.ui.toggle(true, { label = "Slice (C)", group = "Tools in the wheel" }),
  use_select = rb.ui.toggle(true, { label = "Select (E)", group = "Tools in the wheel" }),
  use_zoom = rb.ui.toggle(false, { label = "Zoom (Z)", group = "Tools in the wheel" }),
  use_playback = rb.ui.toggle(false, { label = "Playback (Y)", group = "Tools in the wheel" }),
  show_toast = rb.ui.toggle(true, { label = "Toast the tool name", group = "Feedback" }),
  reset = rb.ui.toggle(false, {
    label = "Reset to defaults", group = "Reset",
    tooltip = "Options can't press buttons, so this switch resets and turns itself off. Keys keep their values.",
  }),
})

local TOOLS = {
  { id = "use_draw", name = "Draw", key = "P", on = true },
  { id = "use_paint", name = "Paint", key = "B", on = true },
  { id = "use_delete", name = "Delete", key = "D", on = true },
  { id = "use_mute", name = "Mute", key = "T", on = true },
  { id = "use_slice", name = "Slice", key = "C", on = true },
  { id = "use_select", name = "Select", key = "E", on = true },
  { id = "use_zoom", name = "Zoom", key = "Z", on = false },
  { id = "use_playback", name = "Playback", key = "Y", on = false },
}

rb.timer.every(500, function()
  if cfg.reset then
    cfg.invert = false
    cfg.step_ms = 120
    for _, t in ipairs(TOOLS) do
      cfg[t.id] = t.on
    end
    cfg.show_toast = true
    cfg.reset = false
    rb.ui.notify("FL Tool Wheel reset to defaults.", "success")
  end
end)

local function pick(t)
  rb.hid.combo(t.key)
  if cfg.show_toast then
    rb.overlay.toast({ { t.name, "accent", true }, " tool  " .. t.key }, { title = "FL Tool Wheel", duration = 900 })
  end
end

local HOLD = if cfg.hold_button == "Mouse5" then "Mouse5" else "Mouse4"
local current = 0
local last = 0

local function step(dir: number)
  local now = rb.system.time()
  if now - last < (cfg.step_ms or 0) then
    return
  end
  local list = {}
  for _, t in ipairs(TOOLS) do
    if cfg[t.id] then
      table.insert(list, t)
    end
  end
  if #list == 0 then
    rb.overlay.toast("No tools are on in Options.", { title = "FL Tool Wheel", duration = 1500 })
    return
  end
  if cfg.invert then
    dir = -dir
  end
  if current == 0 or current > #list then
    current = if dir > 0 then 1 else #list
  else
    current = ((current - 1 + dir) % #list) + 1
  end
  last = now
  pick(list[current])
end

-- ===== Bind Board v1.3 (shared; paste above your binds) =====
-- Usage: BB.bind("snooze", "Snooze", "LCtrl+LAlt+B", fn)  and  BB.board("board", "LCtrl+LAlt+K")
local BB = { rows = {}, order = {}, busy = false }
local MODS = { "LCtrl", "RCtrl", "LShift", "RShift", "LAlt", "RAlt", "LWin", "RWin" }
local IS_MOD = {}
for _, m in MODS do
  IS_MOD[m] = true
end
local REFUSE = { Escape = "Esc always cancels", Mouse1 = "a plain left click can't be a hotkey" }

local function cur(id)
  return rb.ui.get(id) or BB.rows[id].default
end

local function shown(k) -- hide a pinned device id
  return (string.match(k, "^[^@]+"))
end

local function chord(key)
  local parts = {}
  for _, m in MODS do
    if m ~= key and rb.input.is_down(m) then
      table.insert(parts, m)
    end
  end
  table.insert(parts, key)
  return table.concat(parts, "+")
end

local function apply(id, key)
  local r = BB.rows[id]
  local ok, h = pcall(rb.bind, key, r.fn, r.opts)
  if not ok then
    return false
  end
  if r.handle then
    r.handle:stop()
  end
  r.handle = h
  rb.ui.set(id, key)
  return true
end

local function guarded(opts) -- pause this script's Bind Board binds while capture listens
  local g = {}
  for k, v in opts or {} do
    g[k] = v
  end
  local orig = g.when
  g.when = function(d)
    return not BB.busy and (orig == nil or orig(d))
  end
  return g
end

function BB.bind(id, label, default, fn, opts)
  opts = guarded(opts)
  local r = { label = label, default = default, fn = fn, opts = opts }
  BB.rows[id] = r
  table.insert(BB.order, id)
  local key = cur(id)
  local ok, h = pcall(rb.bind, key, fn, opts)
  if not ok and key ~= default then -- a bad saved key falls back to the default
    ok, h = pcall(rb.bind, default, fn, opts)
    if ok then
      rb.ui.set(id, default)
      rb.overlay.toast(label .. " couldn't use " .. shown(key) .. ", so it's back to " .. default .. ".")
    end
  end
  if ok then
    r.handle = h
  else
    rb.overlay.toast(label .. " has no hotkey. Set one in Edit hotkeys or the Options tab.")
  end
end

local function restart_menu(text)
  rb.overlay.menu({
    { label = "Restart script now", action = function() rb.script.reload() end },
    { label = "Later" },
  }, { title = text, subtitle = "Restart to apply" })
end

local function saved(r, key)
  rb.overlay.toast(r.label .. " is now " .. shown(key) .. ".", { title = "Hotkey saved" })
end

local function swap(id, key, clash, mine)
  local r, r2 = BB.rows[id], BB.rows[clash]
  if r2.handle then
    r2.handle:stop()
  end
  r2.handle = nil
  rb.ui.set(clash, mine) -- saved first, so a restart never doubles a key
  if not apply(id, key) then -- put the other action back on its key
    rb.ui.set(clash, key)
    local ok, h = pcall(rb.bind, key, r2.fn, r2.opts)
    if ok then
      r2.handle = h
    end
    rb.overlay.toast("Couldn't use " .. shown(key) .. ". Nothing changed.")
    return
  end
  if apply(clash, mine) then
    saved(r, key)
    return
  end
  restart_menu("Swapped. " .. r2.label .. " is saved as " .. shown(mine))
end

local function set_key(id, key)
  local r, clash = BB.rows[id], nil
  for _, o in BB.order do
    if o ~= id and cur(o) == key then
      clash = o
    end
  end
  if not clash then
    if apply(id, key) then
      saved(r, key)
      return
    end
    rb.ui.set(id, key)
    restart_menu("Saved " .. shown(key) .. " for " .. r.label)
    return
  end
  local mine = cur(id)
  rb.overlay.menu({
    { label = "Cancel" },
    { label = "Swap the two keys", hint = BB.rows[clash].label .. " → " .. shown(mine),
      action = function() swap(id, key, clash, mine) end },
  }, { title = shown(key) .. " is already " .. BB.rows[clash].label, subtitle = "Nothing changes unless you pick Swap" })
end

local function capture(id)
  local r = BB.rows[id]
  local status = rb.overlay.status("Press the new key for " .. r.label .. "  ·  Esc cancels")
  local hDown, hScroll, timer
  local function done()
    BB.busy = false
    status.remove()
    if hDown then
      hDown:stop()
    end
    if hScroll then
      hScroll:stop()
    end
    if timer then
      timer:stop()
    end
  end
  hDown = rb.on("down", function(key)
    if not BB.busy then
      return
    end
    if IS_MOD[key] then -- let modifiers through; wait for the real key
      return
    end
    done()
    if key == "Escape" then
      rb.overlay.toast("Nothing changed.")
      return false
    end
    if REFUSE[key] and chord(key) == key then
      rb.overlay.toast("Can't use " .. key .. ": " .. REFUSE[key] .. ".")
      return false
    end
    set_key(id, chord(key))
    return false
  end)
  hScroll = rb.on("scroll", function(delta)
    if not BB.busy then
      return
    end
    done()
    set_key(id, chord(delta > 0 and "WheelUp" or "WheelDown"))
    return false
  end)
  timer = rb.timer.after(10000, function()
    if not BB.busy then
      return
    end
    done()
    rb.overlay.toast("No key pressed. Nothing changed.")
  end)
end

local function reset_all()
  local failed = {}
  for _, id in BB.order do
    if not apply(id, BB.rows[id].default) then
      table.insert(failed, BB.rows[id].label)
    end
  end
  if #failed == 0 then
    rb.overlay.toast("Every hotkey is back to its default.")
  else
    rb.overlay.toast("Couldn't reset: " .. table.concat(failed, ", ") .. ".")
  end
end

function BB.open()
  if BB.busy then
    return
  end
  local items = {}
  for _, id in BB.order do
    table.insert(items, {
      label = BB.rows[id].label,
      hint = shown(cur(id)),
      action = function()
        BB.busy = true
        capture(id)
      end,
    })
  end
  table.insert(items, { label = "Reset all to defaults", action = reset_all })
  table.insert(items, { label = "Done" })
  local m, why = rb.overlay.menu(items, { title = "Edit hotkeys", subtitle = "Pick one, then press the new key" })
  if not m then
    rb.overlay.toast("Can't open the editor: " .. why .. ".")
  end
end

function BB.board(id, default)
  BB.bind(id, "Edit hotkeys", default, BB.open)
end
-- ===== end Bind Board =====

local function holding(): boolean
  return rb.input.is_down(HOLD)
end

rb.bind("WheelDown", function()
  step(1)
end, { when = holding })
rb.bind("WheelUp", function()
  step(-1)
end, { when = holding })

BB.bind("menu_key", "Open tool menu", "LCtrl+LShift+T", function()
  local items = {}
  for _, t in ipairs(TOOLS) do
    table.insert(items, {
      label = t.name,
      hint = t.key,
      action = function()
        pick(t)
      end,
    })
  end
  local menu, why = rb.overlay.menu(items, { title = "FL Tool Wheel", subtitle = "Pick a tool" })
  if not menu then
    rb.overlay.toast("Menu unavailable: " .. tostring(why), { title = "FL Tool Wheel" })
  end
end)

-- FL in front: the same process rules as this file's modeline (substring, any case).
local function fl_in_front(): boolean
  local ok, w = pcall(rb.system.window)
  if not ok or not w then
    return false
  end
  local p = string.lower(w.process or "")
  return string.find(p, "fl64", 1, true) ~= nil
    or string.find(p, "fl.exe", 1, true) ~= nil
    or string.find(p, "fl studio", 1, true) ~= nil
end
BB.bind("board", "Edit hotkeys", "LCtrl+LAlt+W", BB.open, { when = fl_in_front })

print("FL Tool Wheel ready. Hold " .. HOLD .. " and scroll, or press " .. (rb.ui.get("menu_key") or "LCtrl+LShift+T") .. ".")

3 files, 15.0 KB.