Scroll Container inertia? (smooth scroll)

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

Hello guys as you see scroll container doesnt have inertia property.
How we can add this functionality to the scroll to smooth scroll effect?

:bust_in_silhouette: Reply From: flurick

This can be done by creating your own scroll-container; create a Control for clipping, under that a Control that will act as the origin and will move around based on input.

So the scene tree would be:
MyCustomScrollBox → origin → big_stuff_that_needs_scrollin

With the MyCustomScrollBox script being:

extends Control

var v = Vector2(0,0) #current velocity
var just_stop_under = 1
var multi = -4 #speed of one input
var is_grabbed = false


func _process(delta):
    v *= 0.9
    if v.length() <= just_stop_under: v = Vector2(0,0)
    $origin.rect_position += v


func _gui_input(event):
    
    if event is InputEventMouseButton:
        match event.button_index:
            BUTTON_MIDDLE:  is_grabbed = event.pressed
    
    if event is InputEventMouseButton:
        match event.button_index:
            BUTTON_WHEEL_DOWN:  v.y += multi
            BUTTON_WHEEL_UP:    v.y -= multi
            BUTTON_WHEEL_RIGHT: v.x += multi
            BUTTON_WHEEL_LEFT:  v.x -= multi

Thanks man, I will try it.

ozergul | 2018-10-15 18:58