Programatically add UI elements

Typically: "How do I... ", "How can I... " questions
Post Reply
markjc
Posts: 3
Joined: 31 Mar 2017, 22:02

Programatically add UI elements

Post by markjc »

I am working on a robot that will have a variable amount of joints at runtime. I would like to have a UI that has once slider per joint to control position. I know I can make a static UI that will have a fixed number of sliders. Is there a way to dynamically create UI elements at runtime depending on the number of joints I have?

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: Programatically add UI elements

Post by coppelia »

Hello,

yes, you can easily do this with the new custom UI functionality. For example:

Code: Select all

    local xml='<ui title="MyJoints">'
    for i=1,#allJoints,1 do
        xml=xml..'<hslider minimum="-180" maximum="180" onchange="sliderChange_callback" id="'..i..'"/>'
    end
    xml=xml..'</ui>'
    myUi=simExtCustomUI_create(xml)
and the function that will be called when a slider is moved:

Code: Select all

sliderChange_callback=function(ui,id,value)
    print("My slider "..id.."just moved!")
end
Cheers

Post Reply