WoW - 设置(和重置)框架锚点

WoW - Setting (and resetting) frame anchors

我有一个非常简单的插件,它以文本形式而不是视觉表示形式显示四条信息——每条信息都在自己的框架上。每一个都可以由用户自行决定打开或关闭。

我希望这些框架都水平地相互锚定。够简单吧?将 Frame2 的左边缘锚定到 Frame 1 的右边缘,以相同的方式将 Frame3 锚定到 Frame2,依此类推。如果禁用 Frame2,则需要将 Frame3 锚定到 Frame1。

我试图 运行 Frame:GetChildren() 在锚框上计算子项并将它们锚定到锚框本身而不是彼此锚定,但是 Frame:GetChildren() returns table 的 table 秒,# 运算符不算 table 秒。

作为奖励,我希望用户能够更改帧的顺序。

如何执行此操作的问题今天困扰了我一整天。也许是睡眠不足,或者是缺乏 Lua 经验。无论哪种方式,任何帮助将不胜感激。

第一部分:组织框架

为不需要的帧分配 near-zero 宽度,所有其他帧将随机播放。 (不要将宽度设置为恰好为零,否则任何锚定它的东西也会隐藏。)

function displayFrames(showFrame1, showFrame2, showFrame3, showFrame4)
    Frame1:SetWidth((showFrame1 and 300) or 0.0001)
    Frame1:SetShown(showFrame1)
    Frame2:SetWidth((showFrame2 and 300) or 0.0001)
    Frame2:SetShown(showFrame2)
    -- etc.
end

为了奖励,re-ordering 帧,将所有帧相对于一个共同的 parent 锚定(相互替代)并手动计算 x 偏移量:

Frame1:SetPoint("LEFT", UIParent, "LEFT", 0, 0)
Frame2:SetPoint("LEFT", UIParent, "LEFT", 300, 0)
Frame3:SetPoint("LEFT", UIParent, "LEFT", 900, 0)  -- will be right of Frame4
Frame4:SetPoint("LEFT", UIParent, "LEFT", 600, 0)

第二部分:GetChildren() return 值

GetChildren() returns 多个值,每个值都是一个 table 代表单个 child(即帧)。如果有四个 children 那么你可以这样做:

local child1, child2, child3, child4 = Frame:GetChildren()

如果您事先不知道有多少 children,请考虑将所有值包装到一个 table 中,以便您可以遍历它

local children = { Frame:GetChildren() }

for __, child in ipairs(children) do
   --do something to each child
end

由于您的目标是实际将每一帧锚定到前一帧,除了将第一帧锚定到其他地方,您将需要使用不同类型的循环:

local children = { Frame:GetChildren() }

-- anchor the first frame if it exists
if (children[1]) then
    children[1]:SetPoint("CENTER", UIParent)
end

-- anchor any remaining frames
for i=2, #children do
    children[i]:SetPoint("LEFT", children[i-1], "RIGHT")
end