In this example, we demonstrate how to dynamically add, move, and remove UI components in UI Builder. You can spawn any built-in or custom component, and set any of the properties, in this example we set the text and position of a button component. It is also possible to spawn components as children of other components, such as groups or tabs.
In this example UI, Users can manage the positioning and text of buttons through simple interactions, with updates immediately reflected in real-time. We also ensure that certain actions, such as moving or removing elements, are only possible when an item is selected, maintaining a smooth user experience.
letdrag=false;constrealSize=200;functiononPointerDown(e){drag=true;area.setPointerCapture(e.pointerId);}// Variable to keep track of toggle statelettoggle=false;/* Function to add a child control to a specific group or tab based on dropdown selection*/functionaddChild(control:Control){switch(dropdown.value){case'Group 1':returngroup1.add(control);case'Group 2':returngroup2.add(control);case'Tab 1':case'Tab 2':returntabs.add(dropdown.value,control);}}// Function to enable or disable buttons based on the selected list valuefunctionenableDisable(){btnMove.disabled=!list.value;btnRemove.disabled=!list.value;}// Event handler for the "Add" button click eventbtnAdd.onClick=()=>{// Create a new button with the specified text and positionconstbtn=newButton({text:text.value,top:vertex.value.y,left:vertex.value.x});// Add a click event listener to the new buttonbtn.onClick=e=>console.log("BUTTON",e.sender.text,"CLICK");/* Add the button's name to the list and set the list's value to the button's name*/list.addItem(btn.name);list.value=btn.name;// Add the button to the appropriate group or tabaddChild(btn);// Enable or disable the move and remove buttonsenableDisable();}// Event handler for the list's change eventlist.onChange=enableDisable;// Event handler for the "Move" button click eventbtnMove.onClick=()=>{// Find the element corresponding to the selected list valueconstelement=findElement(list.valueasstring);// Add the element to the appropriate group or tabaddChild(element);}// Event handler for the "Remove" button click eventbtnRemove.onClick=()=>{// Delete the element corresponding to the selected list valuedeleteElement(list.valueasstring);// Remove the element's name from the list and clear the list's valuelist.removeItem(list.value);list.value=null;}// Event handler for the vertex change eventvertex.onChange=()=>{// Find the element corresponding to the selected list valueconstelement=findElement(list.valueasstring);if(!element)return;// Update the element's position based on the vertex valueselement.top=vertex.value.y;element.left=vertex.value.x;}// Event handler for the text change eventtext.onChange=()=>{// Find the button corresponding to the selected list valueconstelement:Button=findElement(list.valueasstring);if(!element)return;// Update the button's textelement.text=text.value;}// Initial ReadfunctionreadData(){updatePosition('x',vtxPosition.value.x);updatePosition('y',vtxPosition.value.y);}readData();