evolve
商店/共享库

list_editor

The on-screen editor of what a panel shows and in which order.

hunnyc0 个脚本在用
支持源码公开
在脚本中使用
pack.json
"requires": { "list_editor": "^1.0" }
你的 lua
local list_editor = require("lib/list_editor")

玩家无需安装:脚本会自动带上库,取范围内的任一版本。发布前可在开发会话中修改 scripts/lib/list_editor/ 来试验。

API
list_editor.new(opts: table): table#

An editor by id. The same id returns the same editor (a reloaded script keeps its state);

参数
opts table id (required, unique among scripts), title, meta, items, default, config, load, save, unique, max, words, on_change, on_reset
返回
table

its options are replaced by the new ones.

list_editor.editors(): table<string, table>#

Every editor made this session, by id (diagnostics and tests).

返回
table<string, table>
介绍

lib/list_editor

The on-screen editor of what a panel shows and in which order. While the menu is open the panel ends in a +; it opens a popover beside the panel with the chosen entries (drag a row to reorder, its minus removes it), the rest of the catalog below (a click appends), Reset and Done. Every change is saved at once, to the config by default. uTEA's watermark runs on it (#296).

local ListEditor = require("lib/list_editor")
local ed = ListEditor.new({ id = "my_hud", title = "My HUD", default = { "fps" }, items = {
    { key = "fps",  name = "FPS",   label = "FPS", value = function() return tostring(math.floor(client.fps())) end },
    { key = "time", name = "Clock", value = function() return os.date("%H:%M") end },
    { key = "ping", name = "Ping",  label = "Ping", value = function()
        local n = netchannel.stats(); return n and (math.floor(n.ping) .. " ms") end },
} })
return { on_draw = function() ed:panel({ y = 300 }) end }

ed:panel draws the whole panel: a strip of label value segments in the user's order, a ui.panel the user drags in the menu (anchored right by default, so it grows to the left as entries are added). A script that draws its own panel calls ed:attach after drawing it instead, on the same layer: the + is drawn there, the popover one layer above LAYER_LIFT.

local x, y, w, h = ...                       -- your panel, ed:plus_w(h) px left free at its right end
draw_my_panel(x, y, w - ed:plus_w(h), h, ed:hovered())   -- light the entry hovered in the popover
ed:attach(x, y, w, h)

options of new

field what
id required, unique across scripts. The same id returns the same editor (a hot reload keeps it), with the new options. Also the ui.panel id of ed:panel and the prefix of its ui.capture ids (<id>.edit, <id>.plus)
items the catalog: an array, or a function returning one each frame. An item: key (string, no commas), name (popover text: string, {en, ru} or fn(key)), label (prefix in the panel and the preview), value = fn(key) -> string|nil [, tone] (nil: no data now, the entry is skipped in the panel and kept in the list), icon (a ui.draw_icon name) or image (a renderer.image handle) for the row, draw = fn(x, cy, h) -> w to draw its segment itself (called with x nil to measure)
default the keys a fresh install and Reset show
config config key of the list, CSV. Default list_editor.<id>
load, save load() -> keys, save(keys): own storage instead of the config
unique default true. false: a key may repeat (a combo of steps)
max the list's cap; the popover says when it is full
title, meta the popover's header
words chosen (caption over the list), all_added (shown when the catalog is used up)
on_change(keys), on_reset() after every change; after Reset

editor methods

name what
keys() the list, live; change it through the methods below
generation() bumps on every change, for a cached draw
index(key), item(key), value(key), name(key) lookups
add(key), remove(i|key), move(i, j), set(keys), reset() edit and save; add returns false on a repeat (unique) or a full list
is_open(), set_open(on) the popover
hovered() the key under the pointer (or in the hand) in the popover, nil when shut
plus_w(h) the width the + takes on a strip h tall: h while the menu is open, 0 shut
attach(x, y, w, h [, plus]) the + (default a square inset 3 px at the right end; plus = {x, y, size} puts it elsewhere) and the popover. Returns hovered()
panel(opts) the ready-made panel; opts.x (default the screen's right edge - 12), y, h, label, anchor (default "right"). Returns x (left edge), y, w, h

ListEditor.editors() lists every editor of the session by id.

behaviour

  • The popover sits under the panel (above it near the bottom of the screen) and steps off the menu window. It holds ui.capture while open, so a ui.panel under it does not start a drag; a press another script's capture holds is not the editor's. A press outside closes it, and so does the menu closing.
  • One popover at a time: opening one closes the rest, and a press inside an open popover is its own even where another editor's + lies under it.
  • One press per editor per frame: a hot reload that draws the hook twice toggles once.
  • The library keeps one instance across reloads (__EVOLVE_LIB_REG.list_editor), requires only theme/motion and theme/overlay (both optional) and the native api.

test

tools\lua\lua.exe scripts\lib\list_editor\selftest.lua

List ops and storage, custom storage, unique = false and max, the + (menu shut, held press, two draws in one frame), the popover (add, minus, drag, hovered, press outside, menu closing), a foreign capture over the +, panel's right anchor and growth, a restart keeping the order.

源码

1.0.0 版本的代码,即客户端拿到的内容。包内字节码由这些文件编译而来。

init.lua684 行
---@diagnostic disable: undefined-global
--- A list the user edits on screen: what a panel shows and in which order. While the menu is
--- open a + at the panel's right end opens a popover beside it: the chosen entries in order (drag
--- a row to reorder, its minus removes it), the rest of the catalog below (a click appends), Reset
--- and Done. Every change is saved at once. `ed:panel` is the whole panel in one call; `ed:attach`
--- adds the editor to a panel the script draws itself.
---
--- ```lua
--- local ListEditor = require("lib/list_editor")
--- local ed = ListEditor.new({ id = "my_hud", title = "My HUD", default = { "fps" }, items = {
---     { key = "fps",  name = "FPS",   value = function() return tostring(math.floor(client.fps())) end },
---     { key = "time", name = "Clock", value = function() return os.date("%H:%M") end },
--- } })
--- return { on_draw = function() ed:panel({ x = 1900, y = 300 }) end }
--- ```

-- one instance per session: a hot reload of the script that requires this gets the live copy,
-- and two copies of a draw hook share one editor and one press per frame (see #290)
do
    local g = rawget(_G, "__EVOLVE_LIB_REG")
    if g and g.list_editor then return g.list_editor end
end

local floor, max, min, abs = math.floor, math.max, math.min, math.abs

local okm, motion = pcall(require, "theme/motion")
if not okm or type(motion) ~= "table" then motion = nil end
local LAYER_HUD, LAYER_LIFT = 1, 2
do
    local ok, O = pcall(require, "theme/overlay")
    if ok and type(O) == "table" then LAYER_HUD, LAYER_LIFT = O.LAYER_HUD or 1, O.LAYER_LIFT or 2 end
end
local LAYER_POP = LAYER_LIFT + 1   -- the popover: above every panel, a lifted one too

local M = { VERSION = "1.0.0" }
local Ed = {}
Ed.__index = Ed
local EDS = {}          -- id -> editor

-- ---------------------------------------------------------------- input, once a frame
local S = { f = nil, mx = 0, my = 0, held = false, prev = false, press = false, menu = false, sw = 1920, sh = 1080 }

local function frame_id()
    if type(globals) == "table" and type(globals.framecount) == "function" then
        local ok, n = pcall(globals.framecount)
        if ok and type(n) == "number" then return n end
    end
end

local function sample()
    local f = frame_id()
    if f and f == S.f then return S end
    S.f = f
    if type(input) == "table" and input.cursor_position then
        local ok, x, y = pcall(input.cursor_position)
        if ok and type(x) == "number" then S.mx, S.my = x, y or 0 end
    end
    local held = false
    if type(input) == "table" and input.is_key_down then
        local ok, v = pcall(input.is_key_down, "mouse1")
        held = ok and v == true
    end
    S.press = held and not S.prev
    S.prev, S.held = held, held
    local ok, o = pcall(ui.menu_open)
    S.menu = ok and o == true
    if type(renderer) == "table" and renderer.screen_size then
        local ok2, w, h = pcall(renderer.screen_size)
        if ok2 and type(w) == "number" and w > 0 then S.sw, S.sh = w, h end
    end
    return S
end

local function inside(px, py, r)
    return r ~= nil and px >= r[1] and py >= r[2] and px < r[1] + r[3] and py < r[2] + r[4]
end

local function px(v)
    local ok, s = pcall(ui.menu_scale)
    return floor(v * ((ok and type(s) == "number" and s > 0) and s or 1) + 0.5)
end

local RU = false
local function lang()
    local ok, l = pcall(ui.menu_lang)
    RU = ok and l == "ru"
end
local function tr(en, ru) return RU and ru or en end

-- a caption: a string, { en, ru } or a function of the key
local function cap(v, key)
    local t = type(v)
    if t == "string" then return v end
    if t == "table" then return RU and v[2] or v[1] end
    if t == "function" then
        local ok, s = pcall(v, key)
        if ok and type(s) == "string" then return s end
    end
end

local function tween(key, target, dur)
    if motion then return (motion.tween(key, target, dur)) end
    return target
end

-- ---------------------------------------------------------------- storage
local function csv(list) return table.concat(list, ",") end

local function parse(s)
    local out = {}
    for k in tostring(s or ""):gmatch("[^,%s]+") do out[#out + 1] = k end
    return out
end

local function copy(list)
    local out = {}
    for i = 1, #list do out[i] = list[i] end
    return out
end

function Ed:_load()
    local list
    if self.o.load then
        local ok, v = pcall(self.o.load)
        if ok and type(v) == "table" then list = copy(v) end
    elseif type(config) == "table" and config.has and config.read_string then
        local ok, has = pcall(config.has, self.key)
        if ok and has then
            local ok2, s = pcall(config.read_string, self.key, "")
            if ok2 and type(s) == "string" then list = parse(s) end
        end
    end
    list = list or copy(self.o.default or {})
    if self.unique then
        local seen, out = {}, {}
        for i = 1, #list do
            if not seen[list[i]] then seen[list[i]] = true; out[#out + 1] = list[i] end
        end
        list = out
    end
    self.list = list
end

function Ed:_changed()
    self.gen = self.gen + 1
    if self.o.save then pcall(self.o.save, self.list)
    elseif type(config) == "table" and config.write_string then pcall(config.write_string, self.key, csv(self.list)) end
    if self.o.on_change then pcall(self.o.on_change, self.list) end
end

-- ---------------------------------------------------------------- the editor

--- An editor by id. The same id returns the same editor (a reloaded script keeps its state);
--- its options are replaced by the new ones.
---@param opts table id (required, unique among scripts), title, meta, items, default, config, load, save, unique, max, words, on_change, on_reset
---@return table editor
function M.new(opts)
    assert(type(opts) == "table" and type(opts.id) == "string" and opts.id ~= "", "list_editor.new: opts.id is required")
    local ed = EDS[opts.id]
    if not ed then
        ed = setmetatable({ id = opts.id, gen = 0, open = false, hl = nil, drag = nil, pf = nil,
                            plus = { 0, 0, 0, 0 }, rect = { 0, 0, 0, 0 }, by = {}, cat = nil }, Ed)
        EDS[opts.id] = ed
    end
    ed.o = opts
    ed.key = opts.config or ("list_editor." .. opts.id)
    ed.unique = opts.unique ~= false
    ed.cap_pop, ed.cap_plus = opts.id .. ".edit", opts.id .. ".plus"
    ed:_load()
    return ed
end

--- The chosen keys in order. A live table: read it, change it through the editor.
---@return string[]
function Ed:keys() return self.list end

--- Bumps on every change, for a caller that caches what it drew.
---@return integer
function Ed:generation() return self.gen end

--- Position of a key in the list, or nil.
---@param key string
---@return integer|nil
function Ed:index(key)
    local l = self.list
    for i = 1, #l do if l[i] == key then return i end end
end

--- Append a key. Refused (false) when it is already there (unique lists) or the list is full.
---@param key string
---@return boolean
function Ed:add(key)
    if type(key) ~= "string" then return false end
    if self.unique and self:index(key) then return false end
    if self.o.max and #self.list >= self.o.max then return false end
    self.list[#self.list + 1] = key
    self:_changed()
    return true
end

--- Remove the entry at a position, or a key.
---@param at integer|string
---@return boolean
function Ed:remove(at)
    local i = type(at) == "number" and at or self:index(at)
    if not i or not self.list[i] then return false end
    table.remove(self.list, i)
    self:_changed()
    return true
end

--- Move the entry at position i to position j.
---@param i integer
---@param j integer
---@return boolean
function Ed:move(i, j)
    local n = #self.list
    if not self.list[i] then return false end
    j = max(1, min(n, j))
    if j == i then return false end
    local k = table.remove(self.list, i)
    table.insert(self.list, j, k)
    self:_changed()
    return true
end

--- Replace the whole list.
---@param keys string[]
function Ed:set(keys)
    self.list = copy(keys or {})
    self:_changed()
end

--- Back to opts.default; calls opts.on_reset.
function Ed:reset()
    self.list = copy(self.o.default or {})
    self:_changed()
    if self.o.on_reset then pcall(self.o.on_reset) end
end

--- Whether the popover is up.
---@return boolean
function Ed:is_open() return self.open end

--- Open or close the popover (it shows only while the menu is open). Opening one closes the
--- popover of every other editor.
---@param on boolean
function Ed:set_open(on)
    self.open = on == true
    if not self.open then self.drag, self.hl = nil, nil; return end
    for _, e in pairs(EDS) do
        if e ~= self and e.open then e:set_open(false) end
    end
end

-- the catalog this frame: opts.items, or what it returns
function Ed:_catalog()
    local items = self.o.items
    if type(items) == "function" then
        local ok, v = pcall(items)
        items = ok and type(v) == "table" and v or {}
    end
    items = items or {}
    if items ~= self.cat or type(self.o.items) == "function" then
        local by = self.by
        for k in pairs(by) do by[k] = nil end
        for i = 1, #items do
            local it = items[i]
            if type(it) == "table" and it.key then by[it.key] = it end
        end
        self.cat = items
    end
    return items
end

--- The catalog entry of a key this frame, or nil.
---@param key string
---@return table|nil
function Ed:item(key)
    if not self.cat then self:_catalog() end
    return self.by[key]
end

--- What an entry shows now: its value and tone, or nil when it has no data.
---@param key string
---@return string|nil value
---@return string|nil tone
function Ed:value(key)
    local it = self:item(key)
    if not (it and it.value) then return nil end
    local ok, v, tone = pcall(it.value, key)
    if ok and v ~= nil and v ~= false then return tostring(v), tone end
end

--- The name the popover prints for a key.
---@param key string
---@return string
function Ed:name(key)
    local it = self:item(key)
    return (it and cap(it.name, key)) or key
end

--- The key of the row under the pointer in the popover, or of the row in the hand: light it in
--- the panel. nil when the popover is shut.
---@return string|nil
function Ed:hovered() return self.open and self.hl or nil end

--- Width the + takes at the right end of a strip h px tall: 0 while the menu is shut.
---@param h number
---@return number
function Ed:plus_w(h)
    if not sample().menu then return 0 end
    return floor(h + 0.5)
end

-- a press this frame that nobody else's capture holds; one per editor per frame
-- a capture id (script prefix included) that some editor's + holds
local function any_plus(id)
    if type(id) ~= "string" then return false end
    for _, e in pairs(EDS) do
        local c = e.cap_plus
        if id == c or (#id > #c and id:sub(-#c - 1) == "." .. c) then return true end
    end
    return false
end

function Ed:_press(s)
    if not s.press then return false end
    if self.pf == s.f and s.f ~= nil then return false end
    -- an open popover owns its rect: another editor's + under it, drawn later, holds a newer capture
    for _, e in pairs(EDS) do
        if e ~= self and e.open and inside(s.mx, s.my, e.rect) then return false end
    end
    local ok1, a = pcall(ui.captured, s.mx, s.my, self.cap_pop)
    local ok2, b = pcall(ui.captured, s.mx, s.my, self.cap_plus)
    if ok1 and ok2 and a and b
       and not (self.open and inside(s.mx, s.my, self.rect) and any_plus(a)) then return false end
    return true
end

local function claim(r)
    if type(input) == "table" and input.claim_mouse then pcall(input.claim_mouse, r[1], r[2], r[3], r[4]) end
    return r
end

local function push(layer)
    if type(renderer) ~= "table" or type(renderer.push_layer) ~= "function" then return false end
    return (pcall(renderer.push_layer, layer))
end
local function pop(pushed)
    if pushed and type(renderer.pop_layer) == "function" then pcall(renderer.pop_layer) end
end

-- the face's real line height, off the last draw: a nominal one put the text above the icons
local T, FH = {}, {}
local function text(s, x, cy, face, tone, align, max_w)
    T.face, T.tone, T.align, T.max_w, T.alpha = face, tone, align, max_w, 1
    local fh = FH[face] or px(face == "small" and 14 or 17)
    local w, h = ui.draw_text(s, x, floor(cy - fh * 0.5 + 0.5), T)
    if type(h) == "number" and h > 0 then FH[face] = h end
    return w
end

-- screen px of a string in the menu's body face
local function text_w(s)
    local ok, w = pcall(ui.menu_text_width, s, "body")
    local ok2, sc = pcall(ui.menu_scale)
    if ok and type(w) == "number" then return floor(w * ((ok2 and type(sc) == "number") and sc or 1) + 0.5) end
    return #s * px(7)
end

local BTN = {}
local function button(id, x, y, w, h, label, kind, s)
    local hot = s.mx >= x and s.my >= y and s.mx < x + w and s.my < y + h
    BTN.kind, BTN.hover, BTN.pressed, BTN.alpha = kind, hot, hot and s.held, 1
    ui.draw_button(id, x, y, w, h, label, BTN)
    return hot
end

local IC = {}
local function row_icon(it, cx, cy, size, tone)
    if it and it.image and type(renderer) == "table" and renderer.image then
        pcall(renderer.image, it.image, floor(cx - size * 0.5), floor(cy - size * 0.5), size, size)
        return true
    end
    if it and it.icon then
        IC.tone = tone
        return ui.draw_icon(it.icon, cx, cy, size, IC) ~= false
    end
end

function Ed:_preview(key)
    local v = self:value(key)
    if v == nil then return tr("no data now", "нет данных"), "disabled" end
    local it = self.by[key]
    local l = it and cap(it.label, key)
    if l then return l .. " " .. v, "low" end
    return v, "low"
end

-- the popover beside `line` {x, y, w, h}; plus = the + rect
function Ed:_popover(line, s, press)
    local m = px
    local order = self.list
    local cat = self:_catalog()
    local add = self._add or {}
    self._add = add
    local na = 0
    for i = 1, #cat do
        local k = cat[i].key
        if k and not (self.unique and self:index(k)) then na = na + 1; add[na] = k end
    end
    for i = #add, na + 1, -1 do add[i] = nil end
    local full = self.o.max and #order >= self.o.max
    local n_in = #order
    local sw, sh = s.sw, s.sh

    local W, pad, HH, CAP, RH, GAP, FB = m(340), m(12), m(40), m(26), m(32), m(10), m(30)
    local H = HH + m(6) + CAP + max(1, n_in) * RH + GAP + CAP + max(1, na) * RH + GAP + FB + pad
    local x = max(m(8), min(line[1] + line[3] - W, sw - W - m(8)))
    local y = line[2] + line[4] + m(8)
    if y + H > sh - m(8) then y = max(m(8), line[2] - m(8) - H) end
    -- the menu paints over every overlay: step off its window, right first
    local ok, mx0, my0, mw0, mh0 = pcall(ui.menu_rect)
    if ok and type(mx0) == "number" and type(mw0) == "number" and type(my0) == "number" and type(mh0) == "number"
       and x < mx0 + mw0 and x + W > mx0 and y < my0 + mh0 and y + H > my0 then
        if mx0 + mw0 + m(8) + W <= sw - m(8) then x = mx0 + mw0 + m(8)
        elseif mx0 - m(8) - W >= m(8) then x = mx0 - m(8) - W end
    end
    local R = self.rect
    R[1], R[2], R[3], R[4] = x, y, W, H
    pcall(ui.capture, self.cap_pop, x, y, W, H)
    claim(R)
    local mx, my = s.mx, s.my

    if press and not inside(mx, my, R) and not inside(mx, my, line) and not inside(mx, my, self.plus) then
        self:set_open(false)
        return
    end

    local title = cap(self.o.title, self.id) or self.id
    local cy = (ui.draw_window(x, y, W, H, { title = title, meta = self.o.meta, header_h = HH, shadow = true })) or (y + HH)
    cy = cy + m(6)
    local x0, x1 = x + pad, x + W - pad
    local ak = "le." .. self.id .. "."

    local words = self.o.words or {}
    text(cap(words.chosen, self.id) or tr("Shown", "Показано"), x0, cy + CAP * 0.5, "small", "mid")
    text(tr("drag to reorder", "тяните, чтобы переставить"), x1, cy + CAP * 0.5, "small", "low", "right")
    local top = cy + CAP
    local D = self.drag
    if D and not s.held then
        if D.live and D.to then self:move(D.i, D.to) end
        self.drag, D = nil, nil
    end
    if D and not order[D.i] then self.drag, D = nil, nil end
    if D and not D.live and abs(my - D.y0) > m(3) then D.live = true end
    local live = D and D.live
    if live then D.to = max(1, min(n_in, floor((my - D.dy - top) / RH + 0.5) + 1)) end
    if n_in == 0 then
        text(tr("empty: add something below", "пусто: добавьте что-нибудь ниже"), x0, top + RH * 0.5, "body", "low")
    end
    local k, float_y, drop = 0, nil, nil
    for i = 1, n_in do
        local key = order[i]
        local it = self.by[key]
        local ry
        if live and i == D.i then
            float_y = max(top, min(top + (n_in - 1) * RH, my - D.dy))
        else
            k = k + 1
            local pos = k + ((live and k >= D.to) and 1 or 0)
            ry = top + tween(ak .. (self.unique and key or (key .. "." .. i)), (pos - 1) * RH, 0.12)
        end
        if ry then
            local row = { x + m(6), ry, W - m(12), RH }
            local hot = not live and inside(mx, my, row)
            local bs = m(24)
            local mb = { x1 - bs, floor(ry + (RH - bs) * 0.5), bs, bs }
            local on_minus = hot and inside(mx, my, mb)
            if hot then
                self.hl = key
                ui.draw_card(row[1], row[2] + m(1), row[3], row[4] - m(2))
            end
            ui.draw_icon("grip-vertical", x0 + m(7), ry + RH * 0.5, m(16), { tone = hot and "mid" or "low" })
            local lead = row_icon(it, x0 + m(30), ry + RH * 0.5, m(20), "mid") and m(22) or 0
            local v, tone = self:_preview(key)
            local vw = text(v, x1 - bs - m(8), ry + RH * 0.5, "small", tone, "right", m(120)) or 0
            text(self:name(key), x0 + m(22) + lead, ry + RH * 0.5, "body", "high", nil,
                 W - pad * 2 - m(22) - lead - bs - m(16) - vw)
            if on_minus then ui.draw_card(mb[1], mb[2], mb[3], mb[4]) end
            ui.draw_icon("minus", mb[1] + bs * 0.5, mb[2] + bs * 0.5, m(16), { tone = on_minus and "high" or "low" })
            if press and hot then
                if on_minus then drop = i     -- after the loop: it walks the list
                else self.drag = { i = i, key = key, dy = my - ry, y0 = my, live = false } end
            end
        end
    end
    if drop then self:remove(drop); n_in = #order end
    if float_y then
        local key = D.key
        self.hl = key
        ui.draw_lit(x + m(6), float_y + m(1), W - m(12), RH - m(2))
        tween(ak .. (self.unique and key or (key .. "." .. D.i)), float_y - top, 0)
        ui.draw_icon("grip-vertical", x0 + m(7), float_y + RH * 0.5, m(16), { tone = "lit" })
        local lead = row_icon(self.by[key], x0 + m(30), float_y + RH * 0.5, m(20), "lit") and m(22) or 0
        text(self:name(key), x0 + m(22) + lead, float_y + RH * 0.5, "body", "lit")
    end

    local ay = top + max(1, n_in) * RH + GAP
    text(tr("Add", "Добавить"), x0, ay + CAP * 0.5, "small", "mid")
    if full then text(tr("the list is full", "список полон"), x1, ay + CAP * 0.5, "small", "low", "right") end
    local atop = ay + CAP
    if na == 0 then
        text(cap(words.all_added, self.id) or tr("everything is shown", "всё уже показано"), x0, atop + RH * 0.5, "body", "low")
    end
    for i = 1, na do
        local key = add[i]
        local it = self.by[key]
        local ry = atop + (i - 1) * RH
        local row = { x + m(6), ry, W - m(12), RH }
        local hot = not live and not full and inside(mx, my, row)
        if hot then ui.draw_card(row[1], row[2] + m(1), row[3], row[4] - m(2)) end
        ui.draw_icon("plus", x0 + m(7), ry + RH * 0.5, m(16), { tone = full and "disabled" or (hot and "accent" or "mid") })
        local lead = row_icon(it, x0 + m(30), ry + RH * 0.5, m(20), "mid") and m(22) or 0
        local v, tone = self:_preview(key)
        local vw = text(v, x1, ry + RH * 0.5, "small", tone, "right", m(140)) or 0
        text(self:name(key), x0 + m(22) + lead, ry + RH * 0.5, "body", full and "disabled" or (hot and "high" or "mid"), nil,
             W - pad * 2 - m(30) - lead - vw)
        if press and hot then drop = key end
    end
    if type(drop) == "string" then self:add(drop) end

    local fy = atop + max(1, na) * RH + GAP
    local rw_, dw = m(120), m(100)
    local r_hot = button(ak .. "reset", x0, fy, rw_, FB, tr("Reset", "Сбросить"), "plain", s)
    local d_hot = button(ak .. "done", x1 - dw, fy, dw, FB, tr("Done", "Готово"), "primary", s)
    if press and not live then
        if r_hot then self:reset() end
        if d_hot then self:set_open(false) end
    end
    if live then self.hl = D.key end
end

local PL = {}
--- Add the editor to a panel the script draws. Call every frame after drawing the panel, from
--- on_draw, on the panel's layer (the + is drawn there; the popover goes above every panel).
--- While the menu is open it draws the + (at `plus` {x, y, size}, default: a square
--- inset 3 px at the right end of the rect, so leave `ed:plus_w(h)` free there) and, once the +
--- is pressed, the popover beside the rect. The menu closing puts it away.
---@param x number
---@param y number
---@param w number
---@param h number
---@param plus? table {x, y, size}
---@return string|nil hovered the key to light in the panel (see ed:hovered)
function Ed:attach(x, y, w, h, plus)
    self.hl = nil
    local s = sample()
    if not s.menu or type(ui.draw_icon) ~= "function" then
        if self.open then self:set_open(false) end
        return nil
    end
    lang()
    local P = self.plus
    if plus then
        P[1], P[2], P[3], P[4] = plus[1], plus[2], plus[3], plus[3]
    else
        local bs = h - px(6)
        P[1], P[2], P[3], P[4] = x + w - px(3) - bs, y + px(3), bs, bs
    end
    pcall(ui.capture, self.cap_plus, P[1], P[2], P[3], P[4])
    claim(P)
    local press = self:_press(s)
    if press then self.pf = s.f end
    local on_plus = inside(s.mx, s.my, P)
    if press and on_plus then
        self:set_open(not self.open)
        press = false
    end
    -- the + on the caller's layer, with its panel
    if self.open then ui.draw_lit(P[1], P[2], P[3], P[4])
    elseif on_plus then ui.draw_card(P[1], P[2], P[3], P[4]) end
    ui.draw_icon("plus", P[1] + P[3] * 0.5, P[2] + P[4] * 0.5, floor(P[4] * 0.62),
                 { tone = self.open and "lit" or (on_plus and "high" or "mid") })
    if self.open then
        local pushed = push(LAYER_POP)
        local ok, err = pcall(function()
            PL[1], PL[2], PL[3], PL[4] = x, y, w, h
            self:_popover(PL, s, press)
        end)
        pop(pushed)
        if not ok then error(err, 0) end
    end
    return self:hovered()
end

-- ---------------------------------------------------------------- the ready-made panel
local SEG = {}
--- The whole panel: a strip of "label value" segments in the user's order, movable in the menu
--- (ui.panel under the editor's id), with the editor attached. An entry with no data is skipped.
--- An entry's `draw(x, cy, h)` -> w draws it itself instead.
---@param opts? table x, y (x is the right edge unless anchor says otherwise), h, label, anchor
---@return number x
---@return number y
---@return number w
---@return number h
function Ed:panel(opts)
    opts = opts or {}
    local s = sample()
    lang()
    self:_catalog()
    local h = opts.h or px(28)
    local gap = px(10)
    local n, w = 0, px(12)
    for i = 1, #self.list do
        local key = self.list[i]
        local it = self.by[key]
        local d = SEG[n + 1] or {}
        SEG[n + 1] = d
        d.key, d.draw, d.text, d.tone = key, nil, nil, nil
        if it and it.draw then
            d.draw = it.draw
            local ok, dw = pcall(it.draw, nil, nil, h)    -- measure: x nil
            d.w = (ok and type(dw) == "number") and dw or 0
        else
            local v, tone = self:value(key)
            if v then
                local l = it and cap(it.label, key)
                d.text = l and (l .. " " .. v) or v
                d.tone = tone
                d.w = text_w(d.text)
            end
        end
        if d.draw or d.text then
            n = n + 1
            w = w + (n > 1 and gap + 1 + gap or 0) + d.w
        end
    end
    if n == 0 then
        local t = tr("empty", "пусто")
        SEG[1] = SEG[1] or {}
        SEG[1].key, SEG[1].draw, SEG[1].text, SEG[1].tone, SEG[1].w = nil, nil, t, "low", text_w(t)
        n, w = 1, w + SEG[1].w
    end
    w = floor(w + px(12) + self:plus_w(h) + 0.5)
    local x, y, pw, ph = ui.panel(self.id, { label = opts.label or cap(self.o.title, self.id) or self.id,
        x = opts.x or (s.sw - 12), y = opts.y or 200, w = w, h = h, min_w = 20, min_h = 12,
        anchor = opts.anchor or "right" })
    if type(x) ~= "number" then return nil end
    local pushed = push(LAYER_HUD)
    local ok, err = pcall(function()
        ui.draw_window(x, y, w, h, { shadow = false })
        local cx, cy = x + px(12), y + h * 0.5
        local hl = self:hovered()
        for i = 1, n do
            local d = SEG[i]
            if i > 1 then
                renderer.rect(floor(cx + gap), floor(cy - px(7)), 1, px(14), Color(255, 255, 255, 40))
                cx = cx + gap + 1 + gap
            end
            if hl and d.key == hl then ui.draw_lit(cx - px(4), y + px(4), d.w + px(8), h - px(8)) end
            if d.draw then pcall(d.draw, cx, cy, h)
            else text(d.text, cx, cy, "body", d.tone or "high") end
            cx = cx + d.w
        end
        self:attach(x, y, w, h)
    end)
    pop(pushed)
    if not ok then error(err, 0) end
    return x, y, w, h
end

--- Every editor made this session, by id (diagnostics and tests).
---@return table<string, table>
function M.editors() return EDS end

M.Editor = Ed

local reg = rawget(_G, "__EVOLVE_LIB_REG") or {}
rawset(_G, "__EVOLVE_LIB_REG", reg)
reg.list_editor = M
return M
pack.json2 行
{ "kind": "lib", "version": "1.0.0" }
更新日志

这个作品没有版本说明。

使用它的脚本

还没有已发布的脚本需要它。

该作者的其他作品

该作者的其他作品

lotus

作者 hunnyc

The lotus pools: how many lotuses each holds and when the next one grows.

pack.json
"requires": { "lotus": "^1.0" }
你的 lua
local lotus = require("lib/lotus")
0 个脚本在用还没有已发布的版本

order_watch

作者 hunnyc

Watches the orders our scripts send and reports three invariants as a red line with the module names (#1184):

pack.json
"requires": { "order_watch": "^1.0" }
你的 lua
local order_watch = require("lib/order_watch")
0 个脚本在用v1.0.0

order_class

作者 hunnyc

The class an order carries into `hk_order` (#1103): reaction, chain, window, background, owner.

pack.json
"requires": { "order_class": "^1.0" }
你的 lua
local order_class = require("lib/order_class")
0 个脚本在用v1.0.0