It is possible. I did this in one of my scripts.
The solution I came up with works as follows:
Let’s say you have a columnLayout populated with a bunch of buttons (button1, button2,… button10).
The user starts dragging button3 and drops it on button7.
At this point you need to decide for yourself how this works. Should button3’s new position now be before or after button7?
Let’s say we want it to be before, so Button3 should become Button6.
Since there is no way to insert a child UI (e.g a button) at a certain index among the parent’s (e.g. a layout) child-array, we need to get creative.
What I did was:
[ol]
[li]Create 2 temporary columnLayouts (-manage 0, so it’s hidden), e.g. “buttonsTemp” and “dragTemp”.
[/li][li]Starting from the top (index 0), move (i.e. reparent) the original columnLayout’s children, i.e. the buttons, one by one into the buttonsTemp layout. The exception here is the button that was dragged (button3). That one goes into the ‘dragTemp’ layout.
[/li][li]The original layout is now empty, the ‘buttonsTemp’ is a duplicate of its original state (minus the dragged button).
[/li][li]Now, we simply move every button in ‘buttonsTemp’ back into the original layout (the order is retained this way).
[/li]Exception: While moving every button, we look at them and check whether this is the drop button (button7). If so, then before moving it, we insert the dragged button from ‘dragTemp’ in to the original layout, then continue proceeding with the rest.
[li]Done. The original layout now contains a list of buttons with the same indices, except for the dragged button, which now sits right before the dropped button.
[/li][/ol]
Here’s some pseudocode:
create columnLayout: 'originalLayout'
create button: 'button1'(parent=$originalLayout, dropCallback=moveButton())
create button: 'button2'(parent=$originalLayout, dropCallback=moveButton())
create button: 'button3'(parent=$originalLayout, dropCallback=moveButton())
[...]
create button: 'button10'(parent=$originalLayout, dropCallback=moveButton())
list originalLayout.childArray
# button1, button2, button3, button4, button5, button6, button7, button8, button9, button10
def moveButton(dragged, dropped):
create columnLayout: 'tempLayout'
create columnLayout: 'draggedLayout'
# move buttons to tempLayouts
for button in originalLayout:
is button = dragged:
reparent button to draggedLayout
else:
reparent button to tempLayout
# move them back in order
for button in tempLayout
# if this isn't the dropped, just move it
is button != dropped
reparent button to originalLayout
# if this is the dropped button, first insert the dragged button, then proceed as usual
else:
reparent (dragged from draggedLayout) to originalLayout #first the dragged...
reparent button to originalLayout #then the dropped
delete tempLayout, draggedLayout
list originalLayout.childArray
# button1, button2, button4, button5, button6, [B]button3, button7[/B], button8, button9, button10
Hope this helps. Maybe there are better solutions (I did this in MEL), but if you have questions, just reply. I can also show you my original code if you like.