Ghost Ref

@xzenile / ghost-refv1.1.0Public

Float a see-through PNG reference over any app to trace or match against, with a hotkey to show, hide and step its opacity.

Files

Version 1.1.0 (current)

gfx-ghost-ref.luau14.0 KB

--[[
  rebind: min_sdk=3.14.0
  rebind: name=GFX · Ghost Ref
  rebind: version=1.1.0
  rebind: author=PLACEHOLDER
  rebind: description=Float a see-through PNG reference over any app to trace or match against, with a hotkey to show, hide and step its opacity.
  rebind: permissions=none
  rebind: instance=single
]]

-- Ghost Ref v1.1.0 (works over any app)
--   Show / hide hotkey (default Ctrl+Alt+R): floats your PNG over the screen.
--   Opacity step hotkey (default Ctrl+Alt+T): 25% -> 50% -> 75% -> 100% -> 25% ...
--   Ctrl+Alt+Shift+R opens Edit hotkeys (Bind Board) to change these keys from the overlay.
--   Options tab: the image path, opacity slider, size and position. Changes apply within half a second.
--
-- Where the image goes: Rebind scripts can only read files in their own folder or their data folder
--   (rb.file docs: absolute paths raise). Put the PNG in this script's data folder and keep the
--   default path "@data/ref.png", or type another name like "@data/face-sheet.png".
--   "rebind script data gfx-ghost-ref.luau" prints where the data folder is.
-- Limits (rb.overlay docs): PNG only, at most 1,048,576 bytes and 2048 x 2048 pixels.
--   A surface may cover at most 1,000,000 square units, so a big image is drawn scaled down to fit.
-- The overlay stays out of screenshots and Windows recordings (rb.overlay limits), and is hidden over
--   exclusive full-screen games on Windows. It does not take mouse clicks from the app underneath.
-- Hotkeys: show/hide, step opacity and "Edit hotkeys" all go through the shared Bind Board block below.
-- APIs: rb.overlay.surface + image op (3.14.0), rb.overlay.toast/menu/status (3.11.1), rb.file.read_bytes/size/exists
--   and @data paths (3.13.0), rb.ui.text/slider/select/keybind/toggle, rb.bind, rb.timer, rb.script.reload.
--   min_sdk 3.14.0.
-- Platforms: Windows and macOS. No app keys are sent, so there are no vendor shortcuts to cite.

local DEFAULTS = {
  path = "@data/ref.png",
  opacity = 50,
  scale = 100,
  anchor = "center",
}
local TOGGLE_DEFAULT = "LCtrl+LAlt+R"
local STEP_DEFAULT = "LCtrl+LAlt+T"
local BOARD_DEFAULT = "LCtrl+LAlt+LShift+R"

local cfg = rb.ui.schema({
  toggle_key = rb.ui.keybind(TOGGLE_DEFAULT, {
    label = "Show / hide",
    tooltip = "Shows or hides the reference image.",
    group = "Hotkeys",
  }),
  step_key = rb.ui.keybind(STEP_DEFAULT, {
    label = "Step opacity",
    tooltip = "Cycles 25%, 50%, 75%, 100%.",
    group = "Hotkeys",
  }),
  board = rb.ui.keybind(BOARD_DEFAULT, {
    label = "Edit hotkeys",
    tooltip = "Opens the Bind Board over any app: pick a hotkey, then press the new key.",
    group = "Hotkeys",
  }),

  path = rb.ui.text(DEFAULTS.path, {
    label = "Image (PNG)",
    placeholder = "@data/ref.png",
    max_length = 200,
    tooltip = "A PNG in this script's data folder (@data/...) or its own folder. Max 1 MB and 2048 x 2048.",
    group = "Image",
  }),
  opacity = rb.ui.slider(DEFAULTS.opacity, { label = "Opacity", min = 5, max = 100, step = 5, suffix = "%", group = "Image" }),
  scale = rb.ui.slider(DEFAULTS.scale, {
    label = "Size",
    min = 10,
    max = 200,
    step = 5,
    suffix = "%",
    tooltip = "Drawn size relative to the image. Large images are capped to the overlay's size limit.",
    group = "Image",
  }),
  anchor = rb.ui.select(DEFAULTS.anchor,
    { "center", "top-left", "top", "top-right", "left", "right", "bottom-left", "bottom", "bottom-right" },
    { label = "Position", group = "Image" }),

  reset = rb.ui.toggle(false, {
    label = "Reset to defaults",
    tooltip = "Resets the Image settings. Hotkeys reset from Edit hotkeys.",
    group = "Reset",
  }),
})

local MAX_BYTES = 1048576
local MAX_EDGE = 2048
local MAX_AREA = 1000000

local surface = nil
local visible = false
local img = nil -- { png, w, h, path }
local applied = {} -- what the surface currently shows

local function toast(msg)
  rb.overlay.toast(msg, { title = "Ghost Ref", duration = 5000 })
end

-- PNG width and height from the IHDR chunk (bytes 17-24, big-endian).
local function pngSize(data)
  if #data < 24 or data:sub(1, 8) ~= "\137PNG\r\n\26\n" or data:sub(13, 16) ~= "IHDR" then
    return nil
  end
  local function u32(i)
    local a, b, c, d = data:byte(i, i + 3)
    return ((a * 256 + b) * 256 + c) * 256 + d
  end
  return u32(17), u32(21)
end

local function load()
  local path = tostring(cfg.path or "")
  if path == "" then
    toast("Set the image path in Options first.")
    return nil
  end
  local okE, exists = pcall(rb.file.exists, path)
  if not okE or not exists then
    toast("Can't find " .. path .. ". Put the PNG in the script's data folder and use @data/<name>.png.")
    return nil
  end
  local okS, size = pcall(rb.file.size, path)
  if okS and size > MAX_BYTES then
    toast(string.format("%s is %.1f MB. The overlay takes PNGs up to 1 MB.", path, size / MAX_BYTES))
    return nil
  end
  local okR, data = pcall(rb.file.read_bytes, path)
  if not okR or not data then
    toast("Couldn't read " .. path .. ": " .. tostring(data))
    return nil
  end
  if #data > MAX_BYTES then
    toast(path .. " is over 1 MB.")
    return nil
  end
  local w, h = pngSize(data)
  if not w then
    toast(path .. " isn't a PNG file.")
    return nil
  end
  if w > MAX_EDGE or h > MAX_EDGE then
    toast(string.format("%s is %d x %d. The overlay takes up to 2048 x 2048.", path, w, h))
    return nil
  end
  return { png = data, w = w, h = h, path = path }
end

local function drawSize()
  local s = cfg.scale / 100
  local w, h = img.w * s, img.h * s
  if w * h > MAX_AREA then
    local f = math.sqrt(MAX_AREA / (w * h))
    w, h = w * f, h * f
  end
  w = math.clamp(math.floor(w), 1, 4096)
  h = math.clamp(math.floor(h), 1, 4096)
  return w, h
end

local function removeSurface()
  if surface then
    pcall(function()
      surface:remove()
    end)
    surface = nil
  end
end

local function hide()
  removeSurface()
  visible = false
  applied = {}
end

local function show()
  img = load()
  if not img then
    hide()
    return
  end
  local w, h = drawSize()
  removeSurface()
  local ok, s = pcall(rb.overlay.surface, {
    width = w,
    height = h,
    anchor = cfg.anchor,
    opacity = cfg.opacity / 100,
  })
  if not ok then
    toast("Couldn't open the overlay: " .. tostring(s))
    hide()
    return
  end
  surface = s
  surface:draw({ { kind = "image", x = 0, y = 0, w = w, h = h, png = img.png } })
  visible = true
  applied = { path = img.path, scale = cfg.scale, anchor = cfg.anchor, opacity = cfg.opacity }
end

local function toggleShown()
  if visible then
    hide()
  else
    show()
  end
end

local STEPS = { 25, 50, 75, 100 }
local function stepOpacity()
  local nextV = STEPS[1]
  for _, v in STEPS do
    if v > cfg.opacity then
      nextV = v
      break
    end
  end
  cfg.opacity = nextV
  if surface then
    surface:set({ opacity = nextV / 100 })
    applied.opacity = nextV
  end
  rb.overlay.toast("Opacity " .. nextV .. "%", { title = "Ghost Ref", duration = 1200 })
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 =====

-- Bind Board: every hotkey in this script.
BB.bind("toggle_key", "Show / hide", TOGGLE_DEFAULT, toggleShown)
BB.bind("step_key", "Step opacity", STEP_DEFAULT, stepOpacity)
BB.board("board", BOARD_DEFAULT)

-- Apply Options changes while the image is up.
rb.timer.every(500, function()
  if cfg.reset then
    for k, v in DEFAULTS do
      rb.ui.set(k, v)
    end
    cfg.reset = false
    rb.ui.notify("Image settings reset to defaults.", "success")
  end
  if not visible then
    return
  end
  if cfg.path ~= applied.path or cfg.scale ~= applied.scale or cfg.anchor ~= applied.anchor then
    show()
  elseif cfg.opacity ~= applied.opacity and surface then
    surface:set({ opacity = cfg.opacity / 100 })
    applied.opacity = cfg.opacity
  end
end)

rb.log.info("Ghost Ref ready: " .. tostring(rb.ui.get("toggle_key") or TOGGLE_DEFAULT) .. " shows " .. tostring(cfg.path))

3 files, 17.6 KB.