0

I'm trying this in Godot 4:

# A helper for making a scroll container in a tab bar
# follow the selected tab button.
class_name PS3TabBarScrollFollow

static func apply(tab_bar: PS3TabBar, container: ScrollContainer) -> void:
    tab_bar.tab_changed.connect(func(_tab):
        var btn = tab_bar.current_tab_button
        container.scroll_horizontal = (btn.global_position.x - container.global_position.x))

It works, however it changes the position abruptly if you use the scroll bar with a mouse scroller for example. follow_focus has no effect since I had to implement custom tab bar buttons without using focus.

I think I'm missing a simple condition here. Something like this:

if btn.not_clipped_by(container):
    # do something
    ...

Does someone have a little logic here?

Hydroper
  • 344
  • 2
  • 9

1 Answers1

0

Here's a simple way by fetching the button global X coordinate and comparing it with the global boundaries of the ScrollContainer:

static func apply(tab_bar: PS3TabBar, container: ScrollContainer) -> void:
    tab_bar.tab_changed.connect(func(_tab):
        var tab_bar_start_x = tab_bar.global_position.x
        var tab_bar_end_x = tab_bar_start_x + tab_bar.size.x
        var btn := tab_bar.current_tab_button
        var btn_x := btn.global_position.x
        if (btn_x + btn.size.x) < tab_bar_start_x or btn_x > tab_bar_end_x:
            var btn_rel_x := btn.global_position.x - container.global_position.x
            container.scroll_horizontal = int(btn_rel_x))
Hydroper
  • 344
  • 2
  • 9