Reordering Control nodes with real-time drag and drop

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bloqm

I have a “turn tracker”, a list of icons representing all the units in play and their turn order. I want to allow the player to change the order by clicking on the icon of a friedly unit, and dragging it in another position in the queue, with some restrictions. However, I want the turn-tracker to update in real time:
* When you click and drag an icon across the tracker, its position updates accordingly.
* When you release the icon into a valid position, the change becomes permanent
* Releasing outside the tracker or right-clicking discards any changes.

My tree looks like this:

- HBoxContainer (Container for all unit icons)
	- PanelContainer (Unit)
		- TextureRect (Unit icon)
		- Other UI elements: name Label, health bar, etc
	- PanelContainer (Unit)
	- PanelContainer (Unit)
	- PanelContainer (Unit)

I’m aware of the built-in drag and drap functions (get_drag_data, can_drag_data, drop_data) but I’m unsure how to implement the logic.

This is likely going to require a custom implementation from you. To my knowledge, the built in drag and drop functions operate similarly to dragging icons around your desktop (if it allowed for swapping). The “drag preview” that you can set only shows an icon by the mouse as you’re dragging around the screen and isn’t “aware” of other draggable data on screen like yours would need to be.

timothybrentwood | 2021-12-05 19:22