How do you use set_drag_preview() when TabBar.drag_to_rearrange_enabled is true?

Godot Version

4.2.1 stable

Question

Is possible to use Control.set_drag_preview() with the TabBar node when TabBar.drag_to_rearrange_enabled is true? When drag_to_rearrange_enabled is true _get_drag_data() stops getting called. Is there a previous call I can override? I need to change the preview and _get_drag_data() is also useful to pass data around.

Because TabBar overrides the functions relative to dragging and dropping when enabling TabBar.drag_to_rearrange_enabled the functions won’t be called on your script.

You can still wait for the _notification() NOTIFICATION_DRAG_BEGIN and set the preview there as the notification fires after the _get_drag_data() has been called.

Example:

extends TabBar


@export var color:Color = Color.WHITE


func _notification(what: int) -> void:
	if what == NOTIFICATION_DRAG_BEGIN:
		# _notification() gets called on all nodes so 
		# we need to get the drag data to know if it comes from this TabBar or not
		var drag_data = get_viewport().gui_get_drag_data()
		printt(drag_data, get_path())
		if drag_data.get('from_path', NodePath()) == get_path():
			# If it's the same path then override the drag preview with our own one
			var cr = ColorRect.new()
			cr.color = color
			cr.custom_minimum_size = Vector2(100, 100)
			set_drag_preview(cr)

This work for a TabBar for a TabContainer the path is different so you’ll need to account for that.

1 Like

Thanks a ton! I forgot about notifications… It would be helpful to write notifications related to each method in the docs the only method mentioning them related to dragging was is_drag_successful

After spending some time I can’t figure out how to get the data back.
NOTIFICATION_DRAG_END can tell me that the drag was successful but at this point I cant get the data or where it was dropped I want to drop a tab from one TabBar to another TabBar and both have the flag active… Right now it works perfectly fine from a TabBar to a regular control even with the flag active. My idea right now is to hack it by turning all the drag_to_rearrange_enabled flags off while dragging a tab but leaving its parent TabBar flag active.

At that point I think it would be better to write your own drag and drop functionality and not depend on drag_to_rearrange_enabled.

Using the hack I got it to work, but I can’t tell at which position the tab was dropped with some extra work. I didn’t want to go as far as implementing my own system, but by the time I spent It would have probably been faster.

Either way I got reminded about notifications so that’s a win.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.