Sometimes you want to hide certain elements to not mess up the menu. It may look simple but actually it's rather difficult and complicated. But pui can make this as easy as pie.
:depend(...) is a method of a pui element, which creates a dependence on elements passed as arguments.
Its arguments can be either elements themselves (generally switches), or tables, which contain ref, condition, invert.
In most of the cases, it looks like this: :depend( {ref, condition}, {ref2, condition2}, ... )
Also, if you want to "disable" elements (lock them), you can do it with :depend(true, ...)
It's worth to note that pui::group supports :depend() as well.
Study these examples:
This is a common example of the use of :depend(...). As you can see, there are two labels showing statuses of a switch.
local switch = group:switch("Master switch")
local status1 = group:label("...is enabled!")
local status2 = group:label("...is disabled!")
status1:depend( {switch, true} ) -- or simply :depend(switch)
status2:depend( {switch, false} )
If you want to show your elements when a certain value is selected in a combobox, this can be done as well.
local combo = group:combo("Author of Pride and Prejudice:", {"Leo Tolstoy", "Charles Dickens", "Jane Austen"})
local correct = group:label("Yes! Jane Austen is")
local wrong = group:label("No! Try again")
correct:depend( {combo, "Jane Austen"} )
wrong:depend( {combo, "Jane Austen", true} )
true in the third argument invert the condition. Basically, it will look like this:
not "Jane Austen"
You can define ranges for sliders like this:
local jitter = group:slider("Jitter degree", 0, 100)
local good = group:depend("Good choice")
good:depend( {jitter, 40, 60} ) -- range from 40 to 60
Sometimes the condition may be too complicated. In this case, you can use a function:
local slider = group:slider("Body temperature", 16, 42, 36)
local normal = group:label("You are healthy")
local cold = group:label("You are rather cold!")
local hot = group:label("You are quite hot!")
normal:depend( {slider, function()
return slider.value >= 35 and slider.value <= 37
end} )
cold:depend( {slider, function()
return slider.value < 34
end} )
hot:depend( {slider, function()
return slider.value > 37
end} )
Yes, you can add more conditions calling :depend(..) multiple times. But you don't have to.
Study this example of a password puzzle:
local a = group:combo("A", {"1", "2", "3", "4"} )
local b = group:combo("B", {"1", "2", "3", "4"} )
local c = group:combo("C", {"1", "2", "3", "4"} )
local answer = group:label("yes! 241 is correct")
answer:depend( {a, "2"}, {b, "4"}, {c, "1"} )
There are a lot of ways to use :depend() that I can't even imagine.
:set_event(event, func, condition*)
This methods is used to call functions in events when a certain condition is met. You can unset this behavior via :unset_event(event, func). You can't unset anonymous functions.
local enabled = pui.switch("Main", "Enable", "Name")
local antiaim = function (cmd)
print(cmd.choked_commands)
end
enabled:set_event("createmove", antiaim)
local selector = pui.combo("Main", "Event", {"off", "render", "createmove"})
local render = function (cmd)
print "render"
end
local createmove = function (cmd)
print "createmove"
end
selector:set_event("render", paint_ui, "paint_ui")
selector:set_event("createmove", setup_command, "createmove")
local multiselect = pui.selectable("Main", "Events", {"render", "createmove"})
local render = function ()
print "render"
end
local createmove = function (cmd)
print "createmove"
end
multiselect:set_event("render", render, function (this)
return this:get("render")
end)
multiselect:set_event("createmove", createmove, function (this)
return this:get("createmove")
end)
local enabled = pui.switch("Main", "Enabled")
local func = function (cmd)
print(cmd.choked_commands)
end
enabled:set_event("setup_command", func)
enabled:set_callback(function (this)
if not this.value then
-- will unset the event after you disable the checkbox for the second time
enabled:unset_event("setup_command", func)
end
end)
local enabled = pui.checkbox("CONFIG", "Presets", "Enabled")
enabled:set_event("setup_command", function (cmd)
print(cmd.choked_commands)
end)
-- this won't work because the function is anonymous
enabled:unset_event("setup_command", function (cmd)
print(cmd.choked_commands)
end)