跳到主要内容

Lua 中的对话框

对话框块能做的所有事情都可以用 Lua 完成。对话框脚本分为三部分:新建对话框、添加问题、显示。然后按 id 读取答案。

Dialog:init()
Dialog:setTitle("Farm helper")
Dialog:add(TextView("Set it up once. You can change it later."))
Dialog:add(CheckBox(1, "Collect daily rewards", true))
Dialog:add(NumberInput(2, "Runs", 10):setRange(1, 999))

local mode = RadioGroup(3, 1)
mode:add(RadioButton(1, "Farm"))
mode:add(RadioButton(2, "Arena"))
Dialog:add(mode)

Dialog:add(TapPicker(4, "Tap the Attack button"):showWhen(3, 2))
Dialog:add(GestureRecorder(5, "Play your route once"))

local advanced = Section("Advanced", true)
advanced:add(Slider(6, "Delay (ms)", 0, 1000, 300):setStep(50):setHelp("Wait between actions"))
Dialog:add(advanced)

Dialog:showIfNeeded()

local runs = Dialog:getValueById(2)
local delay = Dialog:getValueById(6)
for i = 1, runs do
if Dialog:getValueById(3) == 2 then
Screen:click(Dialog:getValueById(4), ClickParams())
end
Record:play(Dialog:getValueById(5))
sleep(delay)
end

它显示的对话框如下:

Farm 助手对话框

组成部分​

步骤函数
新建对话框Dialog:init()、Dialog:setTitle()、Dialog:setMessage()
添加问题Dialog:add(),配合 TextView、CheckBox、EditText、NumberInput、Slider、RadioGroup、TapPicker、TapListPicker、GestureRecorder
排列它们Page、Section、Row、Column
微调问题showWhen、hideWhen、setHelp、setRequired、setRange、setStep、showAsDropdown
显示它Dialog:show() 或 Dialog:showIfNeeded()
读取答案Dialog:getValueById()

注意事项​

  • 先调用 Dialog:init()。它会新建一个空对话框,因此一个宏可以显示多个对话框。
  • setter 会返回问题本身,因此可以链式调用:Slider(6, "Delay", 0, 1000):setStep(50):showWhen(1)。
  • 容器先填充再添加:先 page:add(...),再 Dialog:add(page)。
  • 宏中每个问题的 id 必须不同,因为答案按 id 保存。
  • 如果用户按下 Cancel,Dialog:show() 会停止宏。
  • 在 show() 或 showIfNeeded() 之后读取答案。