Skip to content

Commit

Permalink
Merge pull request #197 from Ark2000/interp
Browse files Browse the repository at this point in the history
fix: fps-independent lerp
  • Loading branch information
worron authored Sep 11, 2024
2 parents c345876 + 60b9dce commit 6262247
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
16 changes: 6 additions & 10 deletions addons/panku_console/common/lynx_window2/lynx_window_2.gd
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,15 @@ func _input(e):
func _process(delta: float) -> void:
if !no_move and _is_dragging:
var tp := position + get_local_mouse_position() - _drag_start_position
position = interp(position, tp, transform_interp_speed, delta)
position = PankuUtils.interp(position, tp, transform_interp_speed, delta)
elif !no_resize and _is_resizing:
var ts := size + _resize_btn.get_local_mouse_position() - _resize_start_position
ts.x = min(ts.x, get_viewport_rect().size.x)
ts.y = min(ts.y, get_viewport_rect().size.y)
if !no_resize_x:
size.x = interp(size.x, ts.x, transform_interp_speed, delta)
size.x = PankuUtils.interp(size.x, ts.x, transform_interp_speed, delta)
if !no_resize_y:
size.y = interp(size.y, ts.y, transform_interp_speed, delta)
size.y = PankuUtils.interp(size.y, ts.y, transform_interp_speed, delta)
elif !no_snap:
var window_rect := get_rect()
var screen_rect := get_viewport_rect()
Expand All @@ -287,14 +287,10 @@ func _process(delta: float) -> void:
if window_rect.end.x > screen_rect.end.x:
target_position.x = screen_rect.end.x - window_rect.size.x
var current_position = window_rect.position
current_position = interp(current_position, target_position, bounds_interp_speed, delta)
size = interp(size, target_size, bounds_interp_speed, delta)
current_position = PankuUtils.interp(current_position, target_position, bounds_interp_speed, delta)
size = PankuUtils.interp(size, target_size, bounds_interp_speed, delta)
position = current_position
if _size_animation:
if _target_size.is_equal_approx(size):
_size_animation = false
size = interp(size, _target_size, anim_interp_speed, delta)

# Framerate-independent interpolation.
func interp(from, to, lambda: float, delta: float):
return lerp(from, to, 1.0 - exp(-lambda * delta))
size = PankuUtils.interp(size, _target_size, anim_interp_speed, delta)
4 changes: 2 additions & 2 deletions addons/panku_console/common/smooth_scroll/smooth_scroll.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func _process(delta: float) -> void:

scrollbar.max_value = content.size.y
var scrollbar_value_max = max(0, scrollbar.max_value - clip_container.size.y)
scrollbar.value = lerp(scrollbar.value, clampf(scrollbar.value, 0.0, scrollbar_value_max), 0.2)
scrollbar.value = PankuUtils.interp(scrollbar.value, clampf(scrollbar.value, 0.0, scrollbar_value_max), 10, delta)
scrollbar.page = clip_container.size.y
scrollbar.visible = content.size.y > clip_container.size.y

scroll_progress = lerp(scroll_progress, scrollbar.value, 0.2)
scroll_progress = PankuUtils.interp(scroll_progress, scrollbar.value, 10, delta)
content.position.y = - scroll_progress

if !follow_content: return
Expand Down
9 changes: 9 additions & 0 deletions addons/panku_console/common/utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ static func get_commit_url() -> String:
return "https://github.com/Ark2000/PankuConsole/commit/" + sha
return ""

# Framerate-independent interpolation.
static func interp(from, to, lambda: float, delta: float):
if from is Vector2:
if abs(from.x - to.x) < 1.0: from.x = to.x
if abs(from.y - to.y) < 1.0: from.y = to.y
if from is float:
if abs(from - to) < 0.01: from = to
return lerp(from, to, 1.0 - exp(-lambda * delta))

#func _run():
# print("plugin_version: ", get_plugin_version())
# print("commit_sha: ", get_commit_sha())
Expand Down

0 comments on commit 6262247

Please sign in to comment.