Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dragging offset and simplify damping formula #33

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 3 additions & 10 deletions addons/SmoothScroll/SmoothScrollContainer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -340,33 +340,28 @@ func handle_scrollbar_drag() -> bool:

func handle_content_dragging():
var calculate_dest = func(delta: float, damping: float) -> float:
var a = (1 - damping * 0.5 - 0.1)
if delta >= 0.0:
return pow(delta + pow(a, 1/(1-a)), a) - pow(pow(a, 1/1-a), a)
return delta / (1 + delta * damping * 0.1)
else:
return delta

var calculate_position = func(
distance1: float, # Realtime distance
distance2: float,
temp_dist1: float, # Temp distance
temp_dist2: float,
temp_relative: float # Event's relative movement accumulation
) -> float:
if distance1 > 0.0:
if temp_relative + temp_dist1 > 0.0:
var delta = min(temp_relative, temp_relative + temp_dist1)
var dest = calculate_dest.call(delta, damping_drag)
return dest - min(0.0, temp_dist1)
elif distance2 < 0.0:
elif temp_relative + temp_dist2 < 0.0:
var delta = max(temp_relative, temp_relative + temp_dist2)
var dest = -calculate_dest.call(-delta, damping_drag)
return dest - max(0.0, temp_dist2)
else: return temp_relative

if allow_vertical_scroll:
var y_pos = calculate_position.call(
top_distance,
bottom_distance,
drag_temp_data[2], # Temp top_distance
drag_temp_data[3], # Temp bottom_distance
drag_temp_data[1] # Temp y relative accumulation
Expand All @@ -375,8 +370,6 @@ func handle_content_dragging():
pos.y = y_pos
if allow_horizontal_scroll:
var x_pos = calculate_position.call(
left_distance,
right_distance,
drag_temp_data[4], # Temp left_distance
drag_temp_data[5], # Temp right_distance
drag_temp_data[0] # Temp x relative accumulation
Expand Down