wolfhacklua API 1.1 Forum

Custom watermark

A small panel in a corner of the screen showing text you choose, the frame rate, the clock and optionally the map. It fades in and out when you toggle it.

Permissions: none. API: 1.0.

Custom watermark v1.0.0 no permissions
Download watermark.lua
watermark.lualua
--@name Custom watermark
--@version 1.0.0
--@author wolfhack docs
--@api 1.0
--@description A small watermark with your own text, the frame rate, the clock and the map. Configure it on its tab on the Scripts page.
-- No permissions needed. Install: Scripts > paste the path of this file > Install, then switch the script on.

local TEXT_SIZE = 14
local PAD_X, PAD_Y = 10, 6
local CORNERS = { "Top left", "Top right", "Bottom left", "Bottom right" }

local fade = 0          -- 0..1, follows the "show" setting so the watermark fades instead of popping

-- The tab. Every control has an id, so its value is saved and read back with store.get(id, default).
ui.tab("Watermark", function()
  ui.card("Content", function()
    ui.toggle("show", "Show the watermark", true)
    ui.input("text", "Text", "wolfhack")
    ui.toggle("show_fps", "Frame rate", true)
    ui.toggle("show_time", "Clock", true)
    ui.toggle("show_map", "Map name", false)
  end, { width = 0.5 })

  ui.card("Look", function()
    ui.dropdown("corner", "Corner", CORNERS, 2)
    ui.slider("margin", "Margin", 0, 80, 16, { int = true, format = "%d px" })
    ui.color("accent", "Accent line", 0x750851FF)
    ui.slider("opacity", "Background opacity", 0, 1, 0.85)
  end, { width = 0.5 })
end)

-- Builds the line of text from the settings.
local function build_text()
  local parts = { store.get("text", "wolfhack") }
  if store.get("show_fps", true) then
    parts[#parts + 1] = string.format("%d fps", math.floor(app.fps() + 0.5))
  end
  if store.get("show_time", true) then
    parts[#parts + 1] = os.date("%H:%M", time.wall())
  end
  if store.get("show_map", false) then
    local snap = data.snapshot()
    if snap.in_game and snap.map ~= "" then parts[#parts + 1] = snap.map end
  end
  return table.concat(parts, "  |  ")
end

events.on("draw", function()
  fade = anim.approach(fade, store.get("show", true) and 1 or 0, 14, time.dt())
  if fade < 0.01 then return end

  local text = build_text()
  local w, h = draw.text_size(text, { size = TEXT_SIZE })
  local box_w, box_h = w + PAD_X * 2, h + PAD_Y * 2

  local sw, sh = app.screen_size()
  local margin = store.get("margin", 16)
  local corner = store.get("corner", 2)                      -- 1 top left, 2 top right, 3 bottom left, 4 bottom right
  local x = (corner == 2 or corner == 4) and (sw - box_w - margin) or margin
  local y = (corner == 3 or corner == 4) and (sh - box_h - margin) or margin

  draw.alpha(fade)
  draw.rect(x, y, box_w, box_h, color.alpha(0x101418FF, store.get("opacity", 0.85)), 4)
  draw.rect(x, y + 3, 2, box_h - 6, store.get("accent", 0x750851FF))
  draw.text(x + PAD_X, y + PAD_Y, 0xF0F0F0FF, text, { size = TEXT_SIZE })
  draw.alpha(1)
end)

How it works#

The tab. ui.tab("Watermark", fn) is registered once; the function builds two cards every frame the tab is open. Each control has an id ("show", "text", "corner", ...), so its value is saved in the store and read back anywhere with store.get(id, default). The defaults in store.get match the ones given to the controls.

Building the text. build_text() collects the parts that are switched on and joins them with table.concat. The frame rate comes from app.fps(), the clock from os.date("%H:%M", time.wall()) and the map from data.snapshot().map, which needs no permission.

Measuring and placing. draw.text_size(text, opts) gives the text's width and height, so the panel is exactly as large as its content. app.screen_size() is read every frame, and the position is computed from the chosen corner and margin, so the watermark follows a resolution change.

Fading. fade = anim.approach(fade, target, 14, time.dt()) moves a value toward 1 or 0 at a speed that does not depend on the frame rate. draw.alpha(fade) multiplies the opacity of everything drawn after it, and draw.alpha(1) restores it. When the value is almost 0 the handler returns before measuring or drawing anything.

Cleanup. Nothing to clean up: the script registers one tab and one draw handler, which the host removes when the script stops.

Ideas to extend it#

  • Show the local player's health (data.snapshot()["local"].health).
  • Add a second line with the bomb timer from snap.bomb.seconds.
  • Use theme.current().accent_rgba as the default accent colour so it matches the menu.
API 1.1 · source docs-src/pages/examples/watermark.md